1 krisbash 1.1 /*
2 **==============================================================================
3 **
4 ** Open Management Infrastructure (OMI)
5 **
6 ** Copyright (c) Microsoft Corporation
7 **
8 ** Licensed under the Apache License, Version 2.0 (the "License"); you may not
9 ** use this file except in compliance with the License. You may obtain a copy
10 ** of the License at
11 **
12 ** http://www.apache.org/licenses/LICENSE-2.0
13 **
14 ** THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 ** KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 ** WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 ** MERCHANTABLITY OR NON-INFRINGEMENT.
18 **
19 ** See the Apache 2 License for the specific language governing permissions
20 ** and limitations under the License.
21 **
22 krisbash 1.1 **==============================================================================
23 */
24
25 #ifndef _omi_logbase_h
26 #define _omi_logbase_h
27
28 #include <common.h>
29 #include <stdarg.h>
30 #include <pal/format.h>
31 #include <pal/strings.h>
32
33 BEGIN_EXTERNC
34
35 /* Log levels (default level is OMI_WARNING */
36 typedef enum _Log_Level
37 {
38 OMI_FATAL,
39 OMI_ERROR,
40 OMI_WARNING,
41 OMI_INFO,
42 OMI_DEBUG,
43 krisbash 1.1 OMI_VERBOSE
44 }
45 Log_Level;
46
47 /* Get the current log level */
48 Log_Level Log_GetLevel();
49
50 /*
51 **==============================================================================
52 **
53 ** Log
54 **
55 ** This interface supports writing of log entries to a flat file. The
56 ** following fragment shows how to use the logging facility.
57 **
58 ** Log_Open("/var/log/mylogfile");
59 ** ...
60 ** Log_Put(OMI_ERROR, __FILE__, __LINE__, "bad sock=%u", sock);
61 ** Log_Put(OMI_INFO, __FILE__, __LINE__, "started");
62 ** Log_Put(OMI_DEBUG, __FILE__, __LINE__, "got this far");
63 ** ...
64 krisbash 1.1 ** Log_Close();
65 **
66 ** The first argument of Log_Put() is the log level, which is one of
67 ** the following (in descending order of priority).
68 **
69 ** OMI_FATAL
70 ** OMI_ERROR
71 ** OMI_WARNING
72 ** OMI_INFO
73 ** OMI_DEBUG
74 ** OMI_VERBOSE
75 **
76 ** Passing one of these to Log_SetLevel() affects which entries are
77 ** written to the log and which are ignored. For example, to log only
78 ** OMI_FATAL and OMI_ERROR levels, call Log_SetLevel as follows.
79 **
80 ** Log_SetLevel(OMI_ERROR);
81 **
82 ** Afterwards, attempts to log anything below the OMI_ERROR level are
83 ** ignored. The default log level is OMI_WARNING.
84 **
85 krisbash 1.1 ** The following macros simplify the writing of log entries by allowing
86 ** the caller to omit the level, __FILE__, and __LINE__ arguments.
87 **
88 ** LOGF() - OMI_FATAL
89 ** LOGE() - OMI_ERROR
90 ** LOGW() - OMI_WARNING
91 ** LOGI() - OMI_INFO
92 ** LOGD() - OMI_DEBUG
93 ** LOGV() - OMI_VERBOSE
94 **
95 ** Using these macros reduces the code example above to this.
96 **
97 ** Log_Open("/var/log/mylogfile");
98 ** ...
99 ** LOGE(("bad sock=%u", sock));
100 ** LOGI(("started"));
101 ** LOGD(("got this far"));
102 ** ...
103 ** Log_Close();
104 **
105 ** Notice the use of double parentheses (since macros do not support
106 krisbash 1.1 ** varying argument lists).
107 **
108 ** Log entries have this format:
109 **
110 ** YYYY/MM/DD HH:MM:SS: LEVEL: FILE(LINE): message...
111 **
112 ** The code snippet above produces the following log entries.
113 **
114 ** 2010/01/01 12:43:26: ERROR: erp.c(10): bad sock=14
115 ** 2010/01/01 12:43:27: INFO: erp.c(11): started
116 ** 2010/01/01 12:43:28: DEBUG: erp.c(12): got this far
117 **
118 **==============================================================================
119 */
120
121 /* Is log already open for output to a file or stderr */
122 MI_Boolean Log_IsOpen();
123
124 /* Open 'path' for append (create if it does not exist). */
125 MI_Result Log_Open(
126 const ZChar* path);
127 krisbash 1.1
128 /* Open 'fd' for append. */
129 MI_Result Log_OpenFD(
130 int fd);
131
132 /* Open log and write log entries to standard error */
133 MI_Result Log_OpenStdErr();
134
135 /* Close the log file. */
136 void Log_Close();
137
138 /* Set the log level (ignore all log writes below this level). */
139 void Log_SetLevel(
140 Log_Level level);
141
142 /* Set the level from one of the following strings/numbers:
143 * FATAL/1
144 * ERROR/2
145 * WARNING/3
146 * INFO/4
147 * DEBUG/5
148 krisbash 1.1 *
149 * Examples:
150 * Log_SetLevelFromString("DEBUG");
151 * Log_SetLevelFromString("3");
152 */
153 int Log_SetLevelFromString(_In_z_ const char* level);
154
155 int Log_SetLevelFromPalCharString(_In_z_ const PAL_Char* level);
156
157 /* represents the length of longest loglevel string */
158 #define MAX_LOG_LEVEL_STRING_LENGTH 20
159
160 /* Get a string representing the current log level, which will be one of the
161 * following symbols: FATAL, ERROR, WARNING, INFO, DEBUG.
162 */
163 const char* Log_GetLevelString(Log_Level level);
164
165 /* tells whether the log is routed to stderr */
166 MI_Boolean Log_IsRoutedToStdErr();
167
168 Log_Level Log_GetLevel();
169 krisbash 1.1
170 int Log_Put(
171 Log_Level level,
172 const char* file,
173 MI_Uint32 line,
174 const ZChar* format,
175 ...);
176
177 /* Va_List form of Log_Put. Returns '1' if output required,'0' otherwise*/
178 int Log_VPut(
179 Log_Level level,
180 const char* file,
181 MI_Uint32 line,
182 const ZChar* format,
183 va_list ap);
184
185 /* Private internal function. */
186 #if defined(CONFIG_ENABLE_DEBUG)
187 int __Log_Put1(
188 Log_Level level,
189 const char* file,
190 krisbash 1.1 MI_Uint32 line);
191 #endif
192
193 /* Private internal function. */
194 //#if defined(CONFIG_ENABLE_DEBUG)
195 void __Log_Put2(
196 const ZChar* format,
197 ...);
198 //#endif
199
200 void FilePutLog(
201 int priority,
202 int eventId,
203 const char * file,
204 int line,
205 const PAL_Char* format,
206 ...);
207
208 /* Private internal function. */
209 int _GetCurrentTimeInUsec(MI_Uint64* usec);
210
211 krisbash 1.1 void __Logf(const ZChar* format, ...);
212 void __Loge(const ZChar* format, ...);
213 void __Logw(const ZChar* format, ...);
214 void __Logi(const ZChar* format, ...);
215 void __Logd(const ZChar* format, ...);
216 void __Logv(const ZChar* format, ...);
217
218 /*
219 * This funtion appears as the false condition expression in the macros
220 * below. It suppresses the Windows 'conditional expression is constant'
221 * warning without the use of pragmas (which cannot be used in macros).
222 */
223 INLINE int __LogFalse() { return 0; }
224
225 #if defined(CONFIG_ENABLE_DEBUG)
226 # define __LOGF(ARGS) \
227 do \
228 { \
229 if (__Log_Put1(OMI_FATAL, __FILE__, __LINE__)) \
230 __Log_Put2 ARGS; \
231 } \
232 krisbash 1.1 while (__LogFalse())
233 # define __LOGE(ARGS) \
234 do \
235 { \
236 if (__Log_Put1(OMI_ERROR, __FILE__, __LINE__)) \
237 __Log_Put2 ARGS; \
238 } \
239 while (__LogFalse())
240 # define __LOGW(ARGS) \
241 do \
242 { \
243 if (__Log_Put1(OMI_WARNING, __FILE__, __LINE__)) \
244 __Log_Put2 ARGS; \
245 } \
246 while (__LogFalse())
247 # define __LOGI(ARGS) \
248 do \
249 { \
250 if (__Log_Put1(OMI_INFO, __FILE__, __LINE__)) \
251 __Log_Put2 ARGS; \
252 } \
253 krisbash 1.1 while (__LogFalse())
254 # define __LOGD(ARGS) \
255 do \
256 { \
257 if (__Log_Put1(OMI_DEBUG, __FILE__, __LINE__)) \
258 __Log_Put2 ARGS; \
259 } \
260 while (__LogFalse())
261 # define __LOGV(ARGS) \
262 do \
263 { \
264 if (__Log_Put1(OMI_VERBOSE, __FILE__, __LINE__)) \
265 __Log_Put2 ARGS; \
266 } \
267 while (__LogFalse())
268 #else
269 # define __LOGF(ARGS) __Logf ARGS
270 # define __LOGE(ARGS) __Loge ARGS
271 # define __LOGW(ARGS) __Logw ARGS
272 # define __LOGI(ARGS) __Logi ARGS
273 # define __LOGD(ARGS) __Logd ARGS
274 krisbash 1.1 # define __LOGV(ARGS) __Logv ARGS
275 #endif
276
277 END_EXTERNC
278
279 #endif /* _omi_logbase_h */
280
|