1 #include <opensrf/log.h>
3 static int _osrfLogType = -1;
4 static int _osrfLogFacility = LOG_LOCAL0;
5 static int _osrfLogActFacility = LOG_LOCAL1;
6 static char* _osrfLogFile = NULL;
7 static char* _osrfLogAppname = NULL;
8 static int _osrfLogLevel = OSRF_LOG_INFO;
9 static int _osrfLogActivityEnabled = 1;
10 static int _osrfLogIsClient = 0;
12 static char* _osrfLogXid = NULL; /* current xid */
13 static char* _osrfLogXidPfx = NULL; /* xid prefix string */
15 static void osrfLogSetType( int logtype );
16 static void _osrfLogDetail( int level, const char* filename, int line, char* msg );
17 static void _osrfLogToFile( char* msg, ... );
18 static void _osrfLogSetXid(char* xid);
20 #define OSRF_LOG_GO(f,li,m,l) \
22 VA_LIST_TO_STRING(m); \
23 _osrfLogDetail( l, f, li, VA_BUF );
25 void osrfLogCleanup() {
26 free(_osrfLogAppname);
31 void osrfLogInit( int type, const char* appname, int maxlevel ) {
33 if(appname) osrfLogSetAppname(appname);
34 osrfLogSetLevel(maxlevel);
35 if( type == OSRF_LOG_TYPE_SYSLOG )
36 openlog(_osrfLogAppname, 0, _osrfLogFacility );
39 static void _osrfLogSetXid(char* xid) {
41 if(_osrfLogXid) free(_osrfLogXid);
42 _osrfLogXid = strdup(xid);
46 void osrfLogClearXid() { _osrfLogSetXid(""); }
47 void osrfLogSetXid(char* xid) {
48 if(!_osrfLogIsClient) _osrfLogSetXid(xid);
52 if(_osrfLogIsClient) {
53 static int _osrfLogXidInc = 0; /* increments with each new xid for uniqueness */
56 snprintf(buf, 32, "%s%d", _osrfLogXidPfx, _osrfLogXidInc);
62 char* osrfLogGetXid() {
66 void osrfLogSetIsClient(int is) {
67 _osrfLogIsClient = is;
69 /* go ahead and create the xid prefix so it will be consistent later */
71 memset(buff, 0x0, 32);
72 snprintf(buff, 32, "%d%ld", (int)time(NULL), (long) getpid());
73 _osrfLogXidPfx = buff;
76 /** Sets the type of logging to perform. See log types */
77 static void osrfLogSetType( int logtype ) {
78 if( logtype != OSRF_LOG_TYPE_FILE &&
79 logtype != OSRF_LOG_TYPE_SYSLOG ) {
80 fprintf(stderr, "Unrecognized log type. Logging to stderr\n");
83 _osrfLogType = logtype;
86 void osrfLogSetFile( const char* logfile ) {
88 if(_osrfLogFile) free(_osrfLogFile);
89 _osrfLogFile = strdup(logfile);
92 void osrfLogSetActivityEnabled( int enabled ) {
93 _osrfLogActivityEnabled = enabled;
96 void osrfLogSetAppname( const char* appname ) {
98 if(_osrfLogAppname) free(_osrfLogAppname);
99 _osrfLogAppname = strdup(appname);
101 /* if syslogging, re-open the log with the appname */
102 if( _osrfLogType == OSRF_LOG_TYPE_SYSLOG) {
104 openlog(_osrfLogAppname, 0, _osrfLogFacility);
108 void osrfLogSetSyslogFacility( int facility ) {
109 _osrfLogFacility = facility;
111 void osrfLogSetSyslogActFacility( int facility ) {
112 _osrfLogActFacility = facility;
115 /** Sets the global log level. Any log statements with a higher level
116 * than "level" will not be logged */
117 void osrfLogSetLevel( int loglevel ) {
118 _osrfLogLevel = loglevel;
121 /** Gets the current global log level. **/
122 int osrfLogGetLevel( void ) {
123 return _osrfLogLevel;
126 void osrfLogError( const char* file, int line, const char* msg, ... )
127 { OSRF_LOG_GO(file, line, msg, OSRF_LOG_ERROR); }
128 void osrfLogWarning( const char* file, int line, const char* msg, ... )
129 { OSRF_LOG_GO(file, line, msg, OSRF_LOG_WARNING); }
130 void osrfLogInfo( const char* file, int line, const char* msg, ... )
131 { OSRF_LOG_GO(file, line, msg, OSRF_LOG_INFO); }
132 void osrfLogDebug( const char* file, int line, const char* msg, ... )
133 { OSRF_LOG_GO(file, line, msg, OSRF_LOG_DEBUG); }
134 void osrfLogInternal( const char* file, int line, const char* msg, ... )
135 { OSRF_LOG_GO(file, line, msg, OSRF_LOG_INTERNAL); }
136 void osrfLogActivity( const char* file, int line, const char* msg, ... ) {
137 OSRF_LOG_GO(file, line, msg, OSRF_LOG_ACTIVITY);
138 _osrfLogDetail( OSRF_LOG_INFO, file, line, VA_BUF ); /* also log at info level */
141 /** Actually does the logging */
142 static void _osrfLogDetail( int level, const char* filename, int line, char* msg ) {
144 if( level == OSRF_LOG_ACTIVITY && ! _osrfLogActivityEnabled ) return;
145 if( level > _osrfLogLevel ) return;
147 if(!filename) filename = "";
149 char* l = "INFO"; /* level name */
150 int lvl = LOG_INFO; /* syslog level */
151 int fac = _osrfLogFacility;
159 case OSRF_LOG_WARNING:
174 case OSRF_LOG_INTERNAL:
179 case OSRF_LOG_ACTIVITY:
182 fac = _osrfLogActFacility;
186 char* xid = (_osrfLogXid) ? _osrfLogXid : "";
188 if(_osrfLogType == OSRF_LOG_TYPE_SYSLOG ) {
190 memset(buf, 0x0, 1536);
191 /* give syslog some breathing room, and be cute about it */
192 strncat(buf, msg, 1535);
197 syslog( fac | lvl, "[%s:%ld:%s:%d:%s] %s", l, (long) getpid(), filename, line, xid, buf );
200 else if( _osrfLogType == OSRF_LOG_TYPE_FILE )
201 _osrfLogToFile("[%s:%ld:%s:%d:%s] %s", l, (long) getpid(), filename, line, xid, msg );
206 static void _osrfLogToFile( char* msg, ... ) {
209 if(!_osrfLogFile) return;
210 VA_LIST_TO_STRING(msg);
212 if(!_osrfLogAppname) _osrfLogAppname = strdup("osrf");
213 int l = strlen(VA_BUF) + strlen(_osrfLogAppname) + 36;
219 time_t t = time(NULL);
220 struct tm* tms = localtime(&t);
221 strftime(datebuf, 36, "%Y-%m-%d %H:%M:%S", tms);
223 FILE* file = fopen(_osrfLogFile, "a");
225 fprintf(stderr, "Unable to fopen file %s for writing\n", _osrfLogFile);
229 fprintf(file, "%s %s %s\n", _osrfLogAppname, datebuf, VA_BUF );
230 if( fclose(file) != 0 )
231 osrfLogWarning(OSRF_LOG_MARK, "Error closing log file: %s", strerror(errno));
236 int osrfLogFacilityToInt( char* facility ) {
237 if(!facility) return LOG_LOCAL0;
238 if(strlen(facility) < 6) return LOG_LOCAL0;
239 switch( facility[5] ) {
240 case '0': return LOG_LOCAL0;
241 case '1': return LOG_LOCAL1;
242 case '2': return LOG_LOCAL2;
243 case '3': return LOG_LOCAL3;
244 case '4': return LOG_LOCAL4;
245 case '5': return LOG_LOCAL5;
246 case '6': return LOG_LOCAL6;
247 case '7': return LOG_LOCAL7;