Nilorea Library

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Macros Groups
n_log.c
Go to the documentation of this file.
1 /*\file n_log.c
2 * generic logging system
3 *\author Mickael Castagnier sur la version de Buathier Christophe <christophe.buathier@certira.cnafmail.fr>
4 *\date 2013-03-12
5 */
6
7 #include "n_log.h"
8
10 static int LOG_LEVEL = NOLOG ;
11
13 static FILE *log_file = NULL ;
14
15
20 void set_log_level( const int log_level )
21 {
22 LOG_LEVEL = log_level ;
23 } /* set_log_level() */
24
25
26
31 int get_log_level()
32 {
33 return LOG_LEVEL ;
34 } /* get_log_level() */
35
36
37
42 int set_log_file( char *file )
43 {
44 _assert( file , return FALSE );
45
46 if( !log_file )
47 log_file = fopen( file , "w" );
48 else
49 {
50 fclose( log_file );
51 log_file = fopen( file , "w" );
52 }
53
54 _assert( log_file , return FALSE );
55
56 return TRUE ;
57 } /* set_log_file */
58
59
60
65 FILE *get_log_file()
66 {
67 return log_file ;
68 } /*get_log_level() */
69
70
71
80 void _n_log( int level , const char *file , const char *func , int line , const char *format , ... )
81 {
82 va_list args ;
83
84 FILE *out = NULL ;
85
86 if( !log_file )
87 out = stderr ;
88 else
89 out = log_file ;
90
91 if( get_log_level() != SYSJRNL )
92 {
93 int log_level = get_log_level();
94 switch( log_level )
95 {
96 case LOG_DEBUG:
97 if( level == LOG_DEBUG )
98 {
99 fprintf( out , "DEBUG: %s: %s line %d: " , file , func , line );
100 va_start (args, format);
101 vfprintf( out, format , args );
102 va_end( args );
103 fprintf( out, "\n" );
104 }
105 case LOG_ERR:
106 if( level == LOG_ERR && log_level != LOG_DEBUG )
107 {
108 fprintf( out , "ERROR: %s->%s: " , file , func );
109 va_start (args, format);
110 vfprintf( out, format , args );
111 va_end( args );
112 fprintf( out, "\n" );
113 }
114 else if( level == LOG_ERR && log_level == LOG_DEBUG )
115 {
116 fprintf( out , "ERROR: %s: %s line %d: " , file , func , line );
117 va_start (args, format);
118 vfprintf( out, format , args );
119 va_end( args );
120 fprintf( out, "\n" );
121 }
122 case LOG_INFO:
123 if( level == LOG_INFO )
124 {
125 fprintf( out , "INFO: " );
126 va_start (args, format);
127 vfprintf( out, format , args );
128 va_end( args );
129 fprintf( out , "\n" );
130 }
131 case LOG_NOTICE:
132 if( level == LOG_NOTICE )
133 {
134 fprintf( out , "NOTICE: " );
135 va_start (args, format);
136 vfprintf( out, format , args );
137 va_end( args );
138 fprintf( out, "\n" );
139 }
140 case NOLOG:
141 break;
142 default:
143 fprintf( out , "UNKNOW_LOG_LEVEL: " );
144 va_start (args, format);
145 vfprintf( out, format , args );
146 va_end( args );
147 fprintf( out, "\n" );
148 break ;
149 }
150 }
151 #if defined( LINUX ) || defined( SOLARIS )
152 else
153 {
154 char syslogbuffer[ 1024 ] = "" ;
155 va_start (args, format);
156 vsprintf( syslogbuffer , format , args );
157 va_end( args );
158
159 if( level == LOG_INFO )
160 sysjrnl( LOG_INFO , "%s" , syslogbuffer );
161 if( level == LOG_NOTICE )
162 sysjrnl( LOG_NOTICE , "%s" , syslogbuffer );
163 if( level == LOG_ERR )
164 sysjrnl( LOG_ERR , "%s" , syslogbuffer );
165 }
166 #endif
167 } /* _n_log( ... ) */
168
169
170
178 int open_safe_logging( TS_LOG **log , char *pathname , char *opt )
179 {
180
181 if( (*log) ){
182
183 if( (*log) -> file )
184 return -1000; /* already open */
185
186 return FALSE;
187 }
188
189 if( !pathname ) return FALSE; /* no path/filename */
190
191 Malloc( (*log) , TS_LOG , 1 );
192
193 if( !(*log) )
194 return FALSE;
195
196 pthread_mutex_init( &(*log) -> LOG_MUTEX , NULL );
197
198 (*log) -> file = fopen( pathname , opt );
199
200 if( !(*log) -> file )
201 return FALSE;
202
203 return TRUE;
204
205 }/* open_safe_logging(...) */
206
207
208
215 int write_safe_log( TS_LOG *log , char *pat , ... )
216 {
217 /* argument list */
218 va_list arg;
219 char str[2048];
220
221 if( !log )
222 return FALSE;
223
224 va_start( arg , pat );
225
226 snprintf( str , sizeof(str), pat, arg );
227
228 va_end( arg );
229
230 pthread_mutex_lock( &log -> LOG_MUTEX );
231
232 fprintf( log -> file , "%s" , str );
233
234 pthread_mutex_unlock( &log -> LOG_MUTEX );
235
236 return TRUE;
237
238 } /* write_safe_log( ... ) */
239
240
241
247 int close_safe_logging( TS_LOG *log )
248 {
249 if( !log )
250 return FALSE;
251
252 pthread_mutex_destroy( &log -> LOG_MUTEX );
253
254 fclose( log -> file );
255
256 return TRUE;
257
258 }/* close_safe_logging( ...) */