annotate common/logging.h @ 616:0172740f5c6e

Include user information in windows event log messages
author Andre Heinecke <andre.heinecke@intevation.de>
date Thu, 19 Jun 2014 12:06:47 +0200
parents 2a4f7364ab81
children 5042ace08cba
rev   line source
404
17e1c8f37d72 Add License
Andre Heinecke <aheinecke@intevation.de>
parents: 329
diff changeset
1 /* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik
17e1c8f37d72 Add License
Andre Heinecke <aheinecke@intevation.de>
parents: 329
diff changeset
2 * Software engineering by Intevation GmbH
17e1c8f37d72 Add License
Andre Heinecke <aheinecke@intevation.de>
parents: 329
diff changeset
3 *
17e1c8f37d72 Add License
Andre Heinecke <aheinecke@intevation.de>
parents: 329
diff changeset
4 * This file is Free Software under the GNU GPL (v>=2)
17e1c8f37d72 Add License
Andre Heinecke <aheinecke@intevation.de>
parents: 329
diff changeset
5 * and comes with ABSOLUTELY NO WARRANTY!
17e1c8f37d72 Add License
Andre Heinecke <aheinecke@intevation.de>
parents: 329
diff changeset
6 * See LICENSE.txt for details.
17e1c8f37d72 Add License
Andre Heinecke <aheinecke@intevation.de>
parents: 329
diff changeset
7 */
252
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
8 #ifndef COMMON_LOGGING_H
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
9 #define COMMON_LOGGING_H
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
10
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
11 #ifdef __cplusplus
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
12 extern "C" {
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
13 #endif
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
14
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
15 /*
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
16 * @file
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
17 * @brief Logging and debugging functions
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
18 */
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
19
503
7f2cb85288d0 Move printlasterror into logging
Andre Heinecke <aheinecke@intevation.de>
parents: 404
diff changeset
20 #include <stdio.h>
7f2cb85288d0 Move printlasterror into logging
Andre Heinecke <aheinecke@intevation.de>
parents: 404
diff changeset
21
615
2a4f7364ab81 Add first simple event logging functions
Andre Heinecke <andre.heinecke@intevation.de>
parents: 513
diff changeset
22 /** @def Maximum length of log messages */
2a4f7364ab81 Add first simple event logging functions
Andre Heinecke <andre.heinecke@intevation.de>
parents: 513
diff changeset
23 #define MAX_LOG 511
2a4f7364ab81 Add first simple event logging functions
Andre Heinecke <andre.heinecke@intevation.de>
parents: 513
diff changeset
24
2a4f7364ab81 Add first simple event logging functions
Andre Heinecke <andre.heinecke@intevation.de>
parents: 513
diff changeset
25 /** @def The name used for logging */
2a4f7364ab81 Add first simple event logging functions
Andre Heinecke <andre.heinecke@intevation.de>
parents: 513
diff changeset
26 #define LOG_NAME "TrustBridge"
2a4f7364ab81 Add first simple event logging functions
Andre Heinecke <andre.heinecke@intevation.de>
parents: 513
diff changeset
27
252
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
28 #ifdef WIN32
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
29
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
30 #include <windows.h>
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
31
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
32 /** @brief Gets the localized error message for the last error
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
33 * returned by GetLastError
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
34 *
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
35 * @returns utf8 error message that needs to be freed by the caller.
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
36 **/
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
37
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
38 char *getLastErrorMsg();
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
39
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
40 #endif
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
41
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
42 /**
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
43 * @def DEBUGOUTPUT
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
44 * @brief If defined code for extra debugging output will be generated.
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
45 *
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
46 * Will be defined if current build is not an RELEASE_BUILD.
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
47 */
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
48 #ifndef RELEASE_BUILD
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
49 #define DEBUGOUTPUT
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
50 #endif
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
51
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
52 /**
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
53 * @def DEBUGPREFIX
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
54 * @brief A string prepended to debug output.
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
55 *
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
56 * Should be defined to indicate which module created the output.
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
57 */
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
58 #ifndef DEBUGPREFIX
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
59 #define DEBUGPREFIX ""
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
60 #endif
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
61
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
62 /**
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
63 * @def DEBUGPRINTF(fmt, ...)
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
64 * @brief Debug printf
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
65 *
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
66 * Prints to stderr if DEBUGOUTPUT is defined.
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
67 */
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
68 #ifdef DEBUGOUTPUT
329
b1059360a0c7 Debugprintf with output debug string on windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 287
diff changeset
69 # ifndef WIN32
b1059360a0c7 Debugprintf with output debug string on windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 287
diff changeset
70 # define DEBUGPRINTF(fmt, ...) fprintf(stderr, DEBUGPREFIX "DEBUG: " fmt, ##__VA_ARGS__);
b1059360a0c7 Debugprintf with output debug string on windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 287
diff changeset
71 # else /* WIN32 */
b1059360a0c7 Debugprintf with output debug string on windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 287
diff changeset
72 # define DEBUGPRINTF(fmt, ...) \
b1059360a0c7 Debugprintf with output debug string on windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 287
diff changeset
73 { \
b1059360a0c7 Debugprintf with output debug string on windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 287
diff changeset
74 char buf[512]; \
b1059360a0c7 Debugprintf with output debug string on windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 287
diff changeset
75 snprintf(buf, 511, "DEBUG: " fmt, ##__VA_ARGS__); \
b1059360a0c7 Debugprintf with output debug string on windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 287
diff changeset
76 buf[511] = '\0'; \
b1059360a0c7 Debugprintf with output debug string on windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 287
diff changeset
77 OutputDebugStringA(buf); \
b1059360a0c7 Debugprintf with output debug string on windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 287
diff changeset
78 }
b1059360a0c7 Debugprintf with output debug string on windows.
Andre Heinecke <aheinecke@intevation.de>
parents: 287
diff changeset
79 # endif /* WIN32 */
287
b033b16dd290 Add OutputDebugString macro
Andre Heinecke <aheinecke@intevation.de>
parents: 252
diff changeset
80 #else
b033b16dd290 Add OutputDebugString macro
Andre Heinecke <aheinecke@intevation.de>
parents: 252
diff changeset
81 # define DEBUGPRINTF(fmt, ...)
b033b16dd290 Add OutputDebugString macro
Andre Heinecke <aheinecke@intevation.de>
parents: 252
diff changeset
82 #endif
b033b16dd290 Add OutputDebugString macro
Andre Heinecke <aheinecke@intevation.de>
parents: 252
diff changeset
83
b033b16dd290 Add OutputDebugString macro
Andre Heinecke <aheinecke@intevation.de>
parents: 252
diff changeset
84 /**
b033b16dd290 Add OutputDebugString macro
Andre Heinecke <aheinecke@intevation.de>
parents: 252
diff changeset
85 * @def ERRORPRINTF(fmt, ...)
b033b16dd290 Add OutputDebugString macro
Andre Heinecke <aheinecke@intevation.de>
parents: 252
diff changeset
86 * @brief Debug printf
b033b16dd290 Add OutputDebugString macro
Andre Heinecke <aheinecke@intevation.de>
parents: 252
diff changeset
87 *
b033b16dd290 Add OutputDebugString macro
Andre Heinecke <aheinecke@intevation.de>
parents: 252
diff changeset
88 * Prints an error to stderr
b033b16dd290 Add OutputDebugString macro
Andre Heinecke <aheinecke@intevation.de>
parents: 252
diff changeset
89 */
504
3cf72c5282e8 Redirect errorprintf to output debug string on windows
Andre Heinecke <aheinecke@intevation.de>
parents: 503
diff changeset
90 #ifdef WIN32
3cf72c5282e8 Redirect errorprintf to output debug string on windows
Andre Heinecke <aheinecke@intevation.de>
parents: 503
diff changeset
91 # define ERRORPRINTF(fmt, ...) \
3cf72c5282e8 Redirect errorprintf to output debug string on windows
Andre Heinecke <aheinecke@intevation.de>
parents: 503
diff changeset
92 { \
3cf72c5282e8 Redirect errorprintf to output debug string on windows
Andre Heinecke <aheinecke@intevation.de>
parents: 503
diff changeset
93 char buf[512]; \
3cf72c5282e8 Redirect errorprintf to output debug string on windows
Andre Heinecke <aheinecke@intevation.de>
parents: 503
diff changeset
94 snprintf(buf, 511, "ERROR: " fmt, ##__VA_ARGS__); \
3cf72c5282e8 Redirect errorprintf to output debug string on windows
Andre Heinecke <aheinecke@intevation.de>
parents: 503
diff changeset
95 buf[511] = '\0'; \
3cf72c5282e8 Redirect errorprintf to output debug string on windows
Andre Heinecke <aheinecke@intevation.de>
parents: 503
diff changeset
96 OutputDebugStringA(buf); \
3cf72c5282e8 Redirect errorprintf to output debug string on windows
Andre Heinecke <aheinecke@intevation.de>
parents: 503
diff changeset
97 }
3cf72c5282e8 Redirect errorprintf to output debug string on windows
Andre Heinecke <aheinecke@intevation.de>
parents: 503
diff changeset
98 #else
3cf72c5282e8 Redirect errorprintf to output debug string on windows
Andre Heinecke <aheinecke@intevation.de>
parents: 503
diff changeset
99 # define ERRORPRINTF(fmt, ...) fprintf(stderr, DEBUGPREFIX "ERROR: " fmt, ##__VA_ARGS__);
3cf72c5282e8 Redirect errorprintf to output debug string on windows
Andre Heinecke <aheinecke@intevation.de>
parents: 503
diff changeset
100 #endif
287
b033b16dd290 Add OutputDebugString macro
Andre Heinecke <aheinecke@intevation.de>
parents: 252
diff changeset
101
513
5e2af38266f6 Add documentation for printlasterror
Andre Heinecke <aheinecke@intevation.de>
parents: 504
diff changeset
102 /**
5e2af38266f6 Add documentation for printlasterror
Andre Heinecke <aheinecke@intevation.de>
parents: 504
diff changeset
103 * @def PRINTLASTERROR(msg)
5e2af38266f6 Add documentation for printlasterror
Andre Heinecke <aheinecke@intevation.de>
parents: 504
diff changeset
104 * @brief Prints the last windows error with a custom message
5e2af38266f6 Add documentation for printlasterror
Andre Heinecke <aheinecke@intevation.de>
parents: 504
diff changeset
105 *
5e2af38266f6 Add documentation for printlasterror
Andre Heinecke <aheinecke@intevation.de>
parents: 504
diff changeset
106 * Prints an error to stderr
5e2af38266f6 Add documentation for printlasterror
Andre Heinecke <aheinecke@intevation.de>
parents: 504
diff changeset
107 */
503
7f2cb85288d0 Move printlasterror into logging
Andre Heinecke <aheinecke@intevation.de>
parents: 404
diff changeset
108 #define PRINTLASTERROR(msg) \
7f2cb85288d0 Move printlasterror into logging
Andre Heinecke <aheinecke@intevation.de>
parents: 404
diff changeset
109 char *my_error = getLastErrorMsg(); \
7f2cb85288d0 Move printlasterror into logging
Andre Heinecke <aheinecke@intevation.de>
parents: 404
diff changeset
110 if (my_error) { \
7f2cb85288d0 Move printlasterror into logging
Andre Heinecke <aheinecke@intevation.de>
parents: 404
diff changeset
111 ERRORPRINTF(msg" : %s\n", my_error); \
7f2cb85288d0 Move printlasterror into logging
Andre Heinecke <aheinecke@intevation.de>
parents: 404
diff changeset
112 free (my_error); \
7f2cb85288d0 Move printlasterror into logging
Andre Heinecke <aheinecke@intevation.de>
parents: 404
diff changeset
113 } \
7f2cb85288d0 Move printlasterror into logging
Andre Heinecke <aheinecke@intevation.de>
parents: 404
diff changeset
114 ERRORPRINTF ("Failed to get error information\n");
287
b033b16dd290 Add OutputDebugString macro
Andre Heinecke <aheinecke@intevation.de>
parents: 252
diff changeset
115
b033b16dd290 Add OutputDebugString macro
Andre Heinecke <aheinecke@intevation.de>
parents: 252
diff changeset
116
615
2a4f7364ab81 Add first simple event logging functions
Andre Heinecke <andre.heinecke@intevation.de>
parents: 513
diff changeset
117 /**
2a4f7364ab81 Add first simple event logging functions
Andre Heinecke <andre.heinecke@intevation.de>
parents: 513
diff changeset
118 * @brief log an informational message into the syslog / event log
2a4f7364ab81 Add first simple event logging functions
Andre Heinecke <andre.heinecke@intevation.de>
parents: 513
diff changeset
119 *
2a4f7364ab81 Add first simple event logging functions
Andre Heinecke <andre.heinecke@intevation.de>
parents: 513
diff changeset
120 * The message length is limited to MAX_LOG characters. Log messages
2a4f7364ab81 Add first simple event logging functions
Andre Heinecke <andre.heinecke@intevation.de>
parents: 513
diff changeset
121 * are expected to be in UTF-8 encoding.
2a4f7364ab81 Add first simple event logging functions
Andre Heinecke <andre.heinecke@intevation.de>
parents: 513
diff changeset
122 *
2a4f7364ab81 Add first simple event logging functions
Andre Heinecke <andre.heinecke@intevation.de>
parents: 513
diff changeset
123 * Function paramters are the same as for the printf familiy.
2a4f7364ab81 Add first simple event logging functions
Andre Heinecke <andre.heinecke@intevation.de>
parents: 513
diff changeset
124 */
2a4f7364ab81 Add first simple event logging functions
Andre Heinecke <andre.heinecke@intevation.de>
parents: 513
diff changeset
125 void syslog_info_printf(const char *format, ...);
2a4f7364ab81 Add first simple event logging functions
Andre Heinecke <andre.heinecke@intevation.de>
parents: 513
diff changeset
126
2a4f7364ab81 Add first simple event logging functions
Andre Heinecke <andre.heinecke@intevation.de>
parents: 513
diff changeset
127 /**
2a4f7364ab81 Add first simple event logging functions
Andre Heinecke <andre.heinecke@intevation.de>
parents: 513
diff changeset
128 * @brief log an error message into the syslog / event log
2a4f7364ab81 Add first simple event logging functions
Andre Heinecke <andre.heinecke@intevation.de>
parents: 513
diff changeset
129 *
2a4f7364ab81 Add first simple event logging functions
Andre Heinecke <andre.heinecke@intevation.de>
parents: 513
diff changeset
130 * The message length is limited to MAX_LOG characters. Log messages
2a4f7364ab81 Add first simple event logging functions
Andre Heinecke <andre.heinecke@intevation.de>
parents: 513
diff changeset
131 * are expected to be in UTF-8 encoding.
2a4f7364ab81 Add first simple event logging functions
Andre Heinecke <andre.heinecke@intevation.de>
parents: 513
diff changeset
132 *
2a4f7364ab81 Add first simple event logging functions
Andre Heinecke <andre.heinecke@intevation.de>
parents: 513
diff changeset
133 * Function paramters are the same as for the printf familiy.
2a4f7364ab81 Add first simple event logging functions
Andre Heinecke <andre.heinecke@intevation.de>
parents: 513
diff changeset
134 */
2a4f7364ab81 Add first simple event logging functions
Andre Heinecke <andre.heinecke@intevation.de>
parents: 513
diff changeset
135 void syslog_error_printf(const char *format, ...);
2a4f7364ab81 Add first simple event logging functions
Andre Heinecke <andre.heinecke@intevation.de>
parents: 513
diff changeset
136
252
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
137 #ifdef __cplusplus
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
138 }
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
139 #endif
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
140
bd7fb50078b4 Add logging.h for some logging / debug functions
Andre Heinecke <aheinecke@intevation.de>
parents:
diff changeset
141 #endif /* COMMON_LOGGING_H */

http://wald.intevation.org/projects/trustbridge/