1 #include <opensrf/log.h>
3 #define OSRF_NO_LOG_TYPE -1
5 static int _prevLogType = OSRF_NO_LOG_TYPE;
6 static int _osrfLogType = OSRF_LOG_TYPE_STDERR;
7 static int _osrfLogFacility = LOG_LOCAL0;
8 static int _osrfLogActFacility = LOG_LOCAL1;
9 static char* _osrfLogFile = NULL;
10 static char* _osrfLogAppname = NULL;
11 static int _osrfLogLevel = OSRF_LOG_INFO;
12 static int _osrfLogActivityEnabled = 1;
13 static int _osrfLogIsClient = 0;
15 static char* _osrfLogXid = NULL; /* current xid */
16 static char* _osrfLogXidPfx = NULL; /* xid prefix string */
18 static void osrfLogSetType( int logtype );
19 static void _osrfLogDetail( int level, const char* filename, int line, char* msg );
20 static void _osrfLogToFile( const char* msg, ... );
21 static void _osrfLogSetXid( const char* xid );
23 #define OSRF_LOG_GO(f,li,m,l) \
25 VA_LIST_TO_STRING(m); \
26 _osrfLogDetail( l, f, li, VA_BUF );
28 void osrfLogCleanup( void ) {
29 free(_osrfLogAppname);
30 _osrfLogAppname = NULL;
33 _osrfLogType = OSRF_LOG_TYPE_STDERR;
37 void osrfLogInit( int type, const char* appname, int maxlevel ) {
39 if(appname) osrfLogSetAppname(appname);
40 osrfLogSetLevel(maxlevel);
41 if( type == OSRF_LOG_TYPE_SYSLOG )
42 openlog(_osrfLogAppname, 0, _osrfLogFacility );
45 static void _osrfLogSetXid( const char* xid ) {
47 if(_osrfLogXid) free(_osrfLogXid);
48 _osrfLogXid = strdup(xid);
52 void osrfLogClearXid( void ) { _osrfLogSetXid(""); }
53 void osrfLogSetXid(char* xid) {
54 if(!_osrfLogIsClient) _osrfLogSetXid(xid);
57 void osrfLogMkXid( void ) {
58 if(_osrfLogIsClient) {
59 static int _osrfLogXidInc = 0; /* increments with each new xid for uniqueness */
61 snprintf(buf, sizeof(buf), "%s%d", _osrfLogXidPfx, _osrfLogXidInc);
67 char* osrfLogGetXid( void ) {
71 void osrfLogSetIsClient(int is) {
72 _osrfLogIsClient = is;
74 /* go ahead and create the xid prefix so it will be consistent later */
76 snprintf(buff, sizeof(buff), "%d%ld", (int)time(NULL), (long) getpid());
77 _osrfLogXidPfx = buff;
80 /** Sets the type of logging to perform. See log types */
81 static void osrfLogSetType( int logtype ) {
85 case OSRF_LOG_TYPE_FILE :
86 case OSRF_LOG_TYPE_SYSLOG :
87 case OSRF_LOG_TYPE_STDERR :
88 _osrfLogType = logtype;
91 fprintf(stderr, "Unrecognized log type. Logging to stderr\n");
92 _osrfLogType = OSRF_LOG_TYPE_STDERR;
97 void osrfLogToStderr( void )
99 if( OSRF_NO_LOG_TYPE == _prevLogType ) {
100 _prevLogType = _osrfLogType;
101 _osrfLogType = OSRF_LOG_TYPE_STDERR;
105 void osrfRestoreLogType( void )
107 if( _prevLogType != OSRF_NO_LOG_TYPE ) {
108 _osrfLogType = _prevLogType;
109 _prevLogType = OSRF_NO_LOG_TYPE;
113 void osrfLogSetFile( const char* logfile ) {
115 if(_osrfLogFile) free(_osrfLogFile);
116 _osrfLogFile = strdup(logfile);
119 void osrfLogSetActivityEnabled( int enabled ) {
120 _osrfLogActivityEnabled = enabled;
123 void osrfLogSetAppname( const char* appname ) {
125 if(_osrfLogAppname) free(_osrfLogAppname);
126 _osrfLogAppname = strdup(appname);
128 /* if syslogging, re-open the log with the appname */
129 if( _osrfLogType == OSRF_LOG_TYPE_SYSLOG) {
131 openlog(_osrfLogAppname, 0, _osrfLogFacility);
135 void osrfLogSetSyslogFacility( int facility ) {
136 _osrfLogFacility = facility;
138 void osrfLogSetSyslogActFacility( int facility ) {
139 _osrfLogActFacility = facility;
142 /** Sets the global log level. Any log statements with a higher level
143 * than "level" will not be logged */
144 void osrfLogSetLevel( int loglevel ) {
145 _osrfLogLevel = loglevel;
148 /** Gets the current global log level. **/
149 int osrfLogGetLevel( void ) {
150 return _osrfLogLevel;
153 void osrfLogError( const char* file, int line, const char* msg, ... )
154 { OSRF_LOG_GO(file, line, msg, OSRF_LOG_ERROR); }
155 void osrfLogWarning( const char* file, int line, const char* msg, ... )
156 { OSRF_LOG_GO(file, line, msg, OSRF_LOG_WARNING); }
157 void osrfLogInfo( const char* file, int line, const char* msg, ... )
158 { OSRF_LOG_GO(file, line, msg, OSRF_LOG_INFO); }
159 void osrfLogDebug( const char* file, int line, const char* msg, ... )
160 { OSRF_LOG_GO(file, line, msg, OSRF_LOG_DEBUG); }
161 void osrfLogInternal( const char* file, int line, const char* msg, ... )
162 { OSRF_LOG_GO(file, line, msg, OSRF_LOG_INTERNAL); }
163 void osrfLogActivity( const char* file, int line, const char* msg, ... ) {
164 OSRF_LOG_GO(file, line, msg, OSRF_LOG_ACTIVITY);
165 _osrfLogDetail( OSRF_LOG_INFO, file, line, VA_BUF ); /* also log at info level */
168 /** Actually does the logging */
169 static void _osrfLogDetail( int level, const char* filename, int line, char* msg ) {
171 if( level == OSRF_LOG_ACTIVITY && ! _osrfLogActivityEnabled ) return;
172 if( level > _osrfLogLevel ) return;
174 if(!filename) filename = "";
176 char* label = "INFO"; /* level name */
177 int lvl = LOG_INFO; /* syslog level */
178 int fac = _osrfLogFacility;
186 case OSRF_LOG_WARNING:
201 case OSRF_LOG_INTERNAL:
206 case OSRF_LOG_ACTIVITY:
209 fac = _osrfLogActFacility;
213 char* xid = (_osrfLogXid) ? _osrfLogXid : "";
215 int logtype = _osrfLogType;
216 if( logtype == OSRF_LOG_TYPE_FILE && !_osrfLogFile )
218 // No log file defined? Temporarily reroute to stderr
219 logtype = OSRF_LOG_TYPE_STDERR;
222 if( logtype == OSRF_LOG_TYPE_SYSLOG ) {
225 /* give syslog some breathing room, and be cute about it */
226 strncat(buf, msg, 1535);
231 syslog( fac | lvl, "[%s:%ld:%s:%d:%s] %s", label, (long) getpid(), filename, line, xid, buf );
234 else if( logtype == OSRF_LOG_TYPE_FILE )
235 _osrfLogToFile( "[%s:%ld:%s:%d:%s] %s", label, (long) getpid(), filename, line, xid, msg );
237 else if( logtype == OSRF_LOG_TYPE_STDERR )
238 fprintf( stderr, "[%s:%ld:%s:%d:%s] %s\n", label, (long) getpid(), filename, line, xid, msg );
242 static void _osrfLogToFile( const char* msg, ... ) {
245 if(!_osrfLogFile) return;
246 VA_LIST_TO_STRING(msg);
248 if(!_osrfLogAppname) _osrfLogAppname = strdup("osrf");
251 time_t t = time(NULL);
252 struct tm* tms = localtime(&t);
253 strftime(datebuf, sizeof( datebuf ), "%Y-%m-%d %H:%M:%S", tms);
255 FILE* file = fopen(_osrfLogFile, "a");
258 "Unable to fopen log file %s for writing; logging to standard error\n", _osrfLogFile);
259 fprintf(stderr, "%s %s %s\n", _osrfLogAppname, datebuf, VA_BUF );
264 fprintf(file, "%s %s %s\n", _osrfLogAppname, datebuf, VA_BUF );
265 if( fclose(file) != 0 )
266 fprintf( stderr, "Error closing log file: %s", strerror(errno));
271 int osrfLogFacilityToInt( char* facility ) {
272 if(!facility) return LOG_LOCAL0;
273 if(strlen(facility) < 6) return LOG_LOCAL0;
274 switch( facility[5] ) {
275 case '0': return LOG_LOCAL0;
276 case '1': return LOG_LOCAL1;
277 case '2': return LOG_LOCAL2;
278 case '3': return LOG_LOCAL3;
279 case '4': return LOG_LOCAL4;
280 case '5': return LOG_LOCAL5;
281 case '6': return LOG_LOCAL6;
282 case '7': return LOG_LOCAL7;