3 int __osrfLogType = -1;
4 int __osrfLogFacility = LOG_LOCAL0;
5 int __osrfLogActFacility = LOG_LOCAL1;
6 char* __osrfLogFile = NULL;
7 char* __osrfLogAppname = NULL;
8 int __osrfLogLevel = OSRF_LOG_INFO;
9 int __osrfLogActivityEnabled = 1;
10 int __osrfLogIsClient = 0;
13 int __osrfLogXidInc = 0; /* increments with each new xid for uniqueness */
14 char* __osrfLogXid = NULL; /* current xid */
15 char* __osrfLogXidPfx = NULL; /* xid prefix string */
18 void osrfLogCleanup() {
19 free(__osrfLogAppname);
24 void osrfLogInit( int type, const char* appname, int maxlevel ) {
26 if(appname) osrfLogSetAppname(appname);
27 osrfLogSetLevel(maxlevel);
28 if( type == OSRF_LOG_TYPE_SYSLOG )
29 openlog(__osrfLogAppname, 0, __osrfLogFacility );
32 static void __osrfLogSetXid(char* xid) {
34 if(__osrfLogXid) free(__osrfLogXid);
35 __osrfLogXid = strdup(xid);
39 void osrfLogClearXid() { __osrfLogSetXid(""); }
40 void osrfLogSetXid(char* xid) {
41 if(!__osrfLogIsClient) __osrfLogSetXid(xid);
45 if(__osrfLogIsClient) {
48 snprintf(buf, 32, "%s%d", __osrfLogXidPfx, __osrfLogXidInc);
54 char* osrfLogGetXid() {
58 void osrfLogSetIsClient(int is) {
59 __osrfLogIsClient = is;
61 /* go ahead and create the xid prefix so it will be consistent later */
63 memset(buff, 0x0, 32);
64 snprintf(buff, 32, "%ld%d", (int)time(NULL), (long) getpid());
65 __osrfLogXidPfx = buff;
68 void osrfLogSetType( int logtype ) {
69 if( logtype != OSRF_LOG_TYPE_FILE &&
70 logtype != OSRF_LOG_TYPE_SYSLOG ) {
71 fprintf(stderr, "Unrecognized log type. Logging to stderr\n");
74 __osrfLogType = logtype;
77 void osrfLogSetFile( const char* logfile ) {
79 if(__osrfLogFile) free(__osrfLogFile);
80 __osrfLogFile = strdup(logfile);
83 void osrfLogSetActivityEnabled( int enabled ) {
84 __osrfLogActivityEnabled = enabled;
87 void osrfLogSetAppname( const char* appname ) {
89 if(__osrfLogAppname) free(__osrfLogAppname);
90 __osrfLogAppname = strdup(appname);
92 /* if syslogging, re-open the log with the appname */
93 if( __osrfLogType == OSRF_LOG_TYPE_SYSLOG) {
95 openlog(__osrfLogAppname, 0, __osrfLogFacility);
99 void osrfLogSetSyslogFacility( int facility ) {
100 __osrfLogFacility = facility;
102 void osrfLogSetSyslogActFacility( int facility ) {
103 __osrfLogActFacility = facility;
106 void osrfLogSetLevel( int loglevel ) {
107 __osrfLogLevel = loglevel;
110 void osrfLogError( const char* file, int line, const char* msg, ... )
111 { OSRF_LOG_GO(file, line, msg, OSRF_LOG_ERROR); }
112 void osrfLogWarning( const char* file, int line, const char* msg, ... )
113 { OSRF_LOG_GO(file, line, msg, OSRF_LOG_WARNING); }
114 void osrfLogInfo( const char* file, int line, const char* msg, ... )
115 { OSRF_LOG_GO(file, line, msg, OSRF_LOG_INFO); }
116 void osrfLogDebug( const char* file, int line, const char* msg, ... )
117 { OSRF_LOG_GO(file, line, msg, OSRF_LOG_DEBUG); }
118 void osrfLogInternal( const char* file, int line, const char* msg, ... )
119 { OSRF_LOG_GO(file, line, msg, OSRF_LOG_INTERNAL); }
120 void osrfLogActivity( const char* file, int line, const char* msg, ... ) {
121 OSRF_LOG_GO(file, line, msg, OSRF_LOG_ACTIVITY);
122 _osrfLogDetail( OSRF_LOG_INFO, file, line, VA_BUF ); /* also log at info level */
125 void _osrfLogDetail( int level, const char* filename, int line, char* msg ) {
127 if( level == OSRF_LOG_ACTIVITY && ! __osrfLogActivityEnabled ) return;
128 if( level > __osrfLogLevel ) return;
130 if(!filename) filename = "";
132 char* l = "INFO"; /* level name */
133 int lvl = LOG_INFO; /* syslog level */
134 int fac = __osrfLogFacility;
142 case OSRF_LOG_WARNING:
157 case OSRF_LOG_INTERNAL:
162 case OSRF_LOG_ACTIVITY:
165 fac = __osrfLogActFacility;
169 char* xid = (__osrfLogXid) ? __osrfLogXid : "";
171 if(__osrfLogType == OSRF_LOG_TYPE_SYSLOG ) {
173 memset(buf, 0x0, 1536);
174 /* give syslog some breathing room, and be cute about it */
175 strncat(buf, msg, 1535);
180 syslog( fac | lvl, "[%s:%ld:%s:%d:%s] %s", l, (long) getpid(), filename, line, xid, buf );
183 else if( __osrfLogType == OSRF_LOG_TYPE_FILE )
184 _osrfLogToFile("[%s:%ld:%s:%d:%s] %s", l, (long) getpid(), filename, line, xid, msg );
189 void _osrfLogToFile( char* msg, ... ) {
192 if(!__osrfLogFile) return;
193 VA_LIST_TO_STRING(msg);
195 if(!__osrfLogAppname) __osrfLogAppname = strdup("osrf");
196 int l = strlen(VA_BUF) + strlen(__osrfLogAppname) + 36;
202 time_t t = time(NULL);
203 struct tm* tms = localtime(&t);
204 strftime(datebuf, 36, "%Y-%m-%d %H:%M:%S", tms);
206 FILE* file = fopen(__osrfLogFile, "a");
208 fprintf(stderr, "Unable to fopen file %s for writing\n", __osrfLogFile);
212 fprintf(file, "%s %s %s\n", __osrfLogAppname, datebuf, VA_BUF );
213 if( fclose(file) != 0 )
214 osrfLogWarning(OSRF_LOG_MARK, "Error closing log file: %s", strerror(errno));
219 int osrfLogFacilityToInt( char* facility ) {
220 if(!facility) return LOG_LOCAL0;
221 if(strlen(facility) < 6) return LOG_LOCAL0;
222 switch( facility[5] ) {
223 case '0': return LOG_LOCAL0;
224 case '1': return LOG_LOCAL1;
225 case '2': return LOG_LOCAL2;
226 case '3': return LOG_LOCAL3;
227 case '4': return LOG_LOCAL4;
228 case '5': return LOG_LOCAL5;
229 case '6': return LOG_LOCAL6;
230 case '7': return LOG_LOCAL7;