]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/utils/log.c
Added additional param to all the osrfLog* calls which mark
[Evergreen.git] / OpenSRF / src / utils / log.c
1 #include "log.h"
2
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
11
12 void osrfLogInit( int type, const char* appname, int maxlevel ) {
13         osrfLogSetType(type);
14         if(appname) osrfLogSetAppname(appname);
15         osrfLogSetLevel(maxlevel);
16         if( type == OSRF_LOG_TYPE_SYSLOG ) 
17                 openlog(__osrfLogAppname, 0, __osrfLogFacility );
18 }
19
20 void osrfLogSetType( int logtype ) { 
21         if( logtype != OSRF_LOG_TYPE_FILE &&
22                         logtype != OSRF_LOG_TYPE_SYSLOG ) {
23                 fprintf(stderr, "Unrecognized log type.  Logging to stderr\n");
24                 return;
25         }
26         __osrfLogType = logtype; 
27 }
28
29 void osrfLogSetFile( const char* logfile ) {
30         if(!logfile) return;
31         if(__osrfLogFile) free(__osrfLogFile);
32         __osrfLogFile = strdup(logfile);
33 }
34
35 void osrfLogSetActivityEnabled( int enabled ) {
36         __osrfLogActivityEnabled = enabled;
37 }
38
39 void osrfLogSetAppname( const char* appname ) {
40         if(!appname) return;
41         if(__osrfLogAppname) free(__osrfLogAppname);
42         __osrfLogAppname = strdup(appname);
43
44         /* if syslogging, re-open the log with the appname */
45         if( __osrfLogType == OSRF_LOG_TYPE_SYSLOG) {
46                 closelog();
47                 openlog(__osrfLogAppname, 0, __osrfLogFacility);
48         }
49 }
50
51 void osrfLogSetSyslogFacility( int facility ) {
52         __osrfLogFacility = facility;
53 }
54 void osrfLogSetSyslogActFacility( int facility ) {
55         __osrfLogActFacility = facility;
56 }
57
58 void osrfLogSetLevel( int loglevel ) {
59         __osrfLogLevel = loglevel;
60 }
61
62 void osrfLogError( const char* file, int line, const char* msg, ... ) 
63         { OSRF_LOG_GO(file, line, msg, OSRF_LOG_ERROR); }
64 void osrfLogWarning( const char* file, int line, const char* msg, ... ) 
65         { OSRF_LOG_GO(file, line, msg, OSRF_LOG_WARNING); }
66 void osrfLogInfo( const char* file, int line, const char* msg, ... ) 
67         { OSRF_LOG_GO(file, line, msg, OSRF_LOG_INFO); }
68 void osrfLogDebug( const char* file, int line, const char* msg, ... ) 
69         { OSRF_LOG_GO(file, line, msg, OSRF_LOG_DEBUG); }
70 void osrfLogInternal( const char* file, int line, const char* msg, ... ) 
71         { OSRF_LOG_GO(file, line, msg, OSRF_LOG_INTERNAL); }
72 void osrfLogActivity( const char* file, int line, const char* msg, ... ) { 
73         OSRF_LOG_GO(file, line, msg, OSRF_LOG_ACTIVITY); 
74         _osrfLogDetail( OSRF_LOG_INFO, file, line, VA_BUF ); /* also log at info level */
75 }
76
77 void _osrfLogDetail( int level, const char* filename, int line, char* msg ) {
78
79         if( level == OSRF_LOG_ACTIVITY && ! __osrfLogActivityEnabled ) return;
80         if( level > __osrfLogLevel ) return;
81         if(!msg) return;
82         if(!filename) filename = "";
83
84         char* l = "INFO";               /* level name */
85         int lvl = LOG_INFO;     /* syslog level */
86         int fac = __osrfLogFacility;
87
88         switch( level ) {
89                 case OSRF_LOG_ERROR:            
90                         l = "ERR "; 
91                         lvl = LOG_ERR;
92                         break;
93
94                 case OSRF_LOG_WARNING:  
95                         l = "WARN"; 
96                         lvl = LOG_WARNING;
97                         break;
98
99                 case OSRF_LOG_INFO:             
100                         l = "INFO"; 
101                         lvl = LOG_INFO;
102                         break;
103
104                 case OSRF_LOG_DEBUG:    
105                         l = "DEBG"; 
106                         lvl = LOG_DEBUG;
107                         break;
108
109                 case OSRF_LOG_INTERNAL: 
110                         l = "INT "; 
111                         lvl = LOG_DEBUG;
112                         break;
113
114                 case OSRF_LOG_ACTIVITY: 
115                         l = "ACT"; 
116                         lvl = LOG_INFO;
117                         fac = __osrfLogActFacility;
118                         break;
119         }
120
121         if(__osrfLogType == OSRF_LOG_TYPE_SYSLOG )
122                 syslog( fac | lvl, "[%s:%d:%s:%d] %s", l, getpid(), filename, line, msg );
123
124         else if( __osrfLogType == OSRF_LOG_TYPE_FILE )
125                 _osrfLogToFile("[%s:%d:%s:%d] %s", l, getpid(), filename, line, msg );
126
127 }
128
129
130 void _osrfLogToFile( char* msg, ... ) {
131
132         if(!msg) return;
133         if(!__osrfLogFile) return;
134         VA_LIST_TO_STRING(msg);
135
136         if(!__osrfLogAppname) __osrfLogAppname = strdup("osrf");
137         int l = strlen(VA_BUF) + strlen(__osrfLogAppname) + 36;
138         char buf[l];
139         bzero(buf,l);
140
141         char datebuf[36];
142         bzero(datebuf,36);
143         time_t t = time(NULL);
144         struct tm* tms = localtime(&t);
145         strftime(datebuf, 36, "%Y-%m-%d %H:%M:%S", tms);
146
147         FILE* file = fopen(__osrfLogFile, "a");
148         if(!file) {
149                 fprintf(stderr, "Unable to fopen file %s for writing", __osrfLogFile);
150                 return;
151         }
152
153         fprintf(file, "%s %s %s\n", __osrfLogAppname, datebuf, VA_BUF );
154         if( fclose(file) != 0 ) 
155                 osrfLogWarning(OSRF_LOG_MARK, "Error closing log file: %s", strerror(errno));
156         
157 }
158
159
160 int osrfLogFacilityToInt( char* facility ) {
161         if(!facility) return LOG_LOCAL0;
162         if(strlen(facility) < 6) return LOG_LOCAL0;
163         switch( facility[5] ) {
164                 case '0': return LOG_LOCAL0;
165                 case '1': return LOG_LOCAL1;
166                 case '2': return LOG_LOCAL2;
167                 case '3': return LOG_LOCAL3;
168                 case '4': return LOG_LOCAL4;
169                 case '5': return LOG_LOCAL5;
170                 case '6': return LOG_LOCAL6;
171                 case '7': return LOG_LOCAL7;
172         }
173         return LOG_LOCAL0;
174 }
175
176