]> arthur.barton.de Git - netatalk.git/blob - libatalk/util/logger.c
merged logging code into main branch. use configure option --without-logfile to...
[netatalk.git] / libatalk / util / logger.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 /* ==========================================================================
6
7        logger.c is part of the utils section in the libatalk library, 
8         which is part of the netatalk project.  
9
10        logger.c was written by Simon Bazley (sibaz@sibaz.com)
11
12        I believe libatalk is released under the L/GPL licence.  
13
14        Just incase, it is, thats the licence I'm applying to this file.
15
16        Netatalk 2001 (c)
17
18    ==========================================================================
19
20        Logger.c is intended as an alternative to syslog for logging
21
22        ---------------------------------------------------------------
23
24    The initial plan is to create a structure for general information needed
25     to log to a file.  
26
27    Initally I'll hard code the neccesary stuff to start a log, this should
28     probably be moved elsewhere when some code is written to read the log
29     file locations from the config files.  
30
31    As a more longterm idea, I'll code this so that the data struct can be
32     duplicated to allow multiple concurrent log files, although this is 
33     probably a recipe for wasted resources. 
34
35    ========================================================================== */
36
37 #include <stdio.h>
38 #include <limits.h>
39 #include <stdarg.h>
40 #include <syslog.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <time.h>
44
45 #include <atalk/boolean.h>
46 #include <atalk/logger.h>
47
48 #define COUNT_ARRAY(array) (sizeof((array))/sizeof((array)[0]))
49 #undef  KEEP_LOGFILES_OPEN
50 #define DO_SYSLOG
51 #define DO_FILELOG
52
53 #undef  DEBUG_OUTPUT_TO_SCREEN
54 #undef  CHECK_STAT_ON_NEW_FILES 
55 #undef  CHECK_ACCESS_ON_NEW_FILES 
56
57 /* ==========================================================================
58     External function declarations
59    ========================================================================== */
60
61 /* setup the internal variables used by the logger (called automatically) */
62 void log_init();
63
64 /* Setup the log filename and the loglevel, and the type of log it is. */
65 bool log_setup(char *filename, enum loglevels loglevel, enum logtypes logtype, int display_options);
66
67 /* Setup the Level and type of log that will be logged to syslog. */
68 void syslog_setup(enum loglevels loglevel, enum logtypes logtype, int display_options, int facility);
69
70 /* finish up and close the logs */
71 void log_close();
72
73 /* This function sets up the processname */
74 void set_processname(char *processname);
75
76 /* Log a Message */
77 void make_log(enum loglevels loglevel, enum logtypes logtype, char *message, ...);
78 #ifndef DISABLE_LOGGER
79 make_log_func set_log_location(char *srcfilename, int srclinenumber);
80
81 /* ========================================================================== 
82     Structure definitions
83    ========================================================================== */
84
85 /* A structure containing object level stuff */
86 struct tag_log_file_data {
87   char log_filename[PATH_MAX];  /* Name of file */
88   FILE *log_file;      /* FILE pointer to file */
89   enum loglevels   log_level;     /* Log Level to put in this file */
90   int  display_options;
91 };
92
93 typedef struct tag_log_file_data log_file_data_pair[2];
94
95 /* A structure containg class level stuff */
96 struct tag_global_log_data {
97   int   struct_size;
98
99   char *temp_src_filename;
100   int   temp_src_linenumber;
101   char  processname[16];
102   
103   char *log_file_directory;  /* Path of directory containing log files */
104   log_file_data_pair **logs;
105 };
106
107 struct what_to_print_array {
108   bool print_datetime;
109   bool print_processname;
110   bool print_pid;
111   bool print_srcfile;
112   bool print_srcline;
113   bool print_errlevel;
114   bool print_errtype;
115 };
116
117 /* ==========================================================================
118     Internal function declarations
119    ========================================================================== */
120
121 void generate_message_details(char *message_details_buffer,
122                               int message_details_buffer_length,
123                               struct tag_log_file_data *log_struct,
124                               enum loglevels loglevel, enum logtypes logtype);
125
126 int get_syslog_equivalent(enum loglevels loglevel);
127
128 static char *get_command_name(char *commandpath);
129
130 /* ==========================================================================
131     Instanciated data
132    ========================================================================== */
133
134 /* A populated instance */
135
136 static log_file_data_pair default_log_file_data_pair = {
137 {
138   /* log_filename */ "\0\0\0\0\0\0\0\0",
139   /* log_file     */ NULL,
140   /* log_level    */ log_debug,
141   /* display_options */ logoption_pid
142 },
143 {
144   /* log_filename */ LOGFILEPATH,
145   /* log_file     */ NULL,
146   /* log_level    */ log_debug,
147   /* display_options */ logoption_pid
148 }};
149
150 static log_file_data_pair *log_file_data_array[logtype_end_of_list_marker] = 
151 {&default_log_file_data_pair};
152
153 /* The class (populated) */
154 static struct tag_global_log_data global_log_data = {
155   /* struct_size  */ sizeof(struct tag_global_log_data),
156   /* temp_src_filename */ NULL,
157   /* temp_src_linenumber */ 0,
158   /* processname */ "",
159   /* log_file_directory */ "",
160   /* logs */  NULL
161 };
162
163 /* macro to get access to the array */
164 #define log_file_arr (global_log_data.logs)
165
166 /* Array to store text to list given a log type */
167 static const char * arr_logtype_strings[] =  LOGTYPE_STRING_IDENTIFIERS;
168 static const int num_logtype_strings = COUNT_ARRAY(arr_logtype_strings);
169
170 /* Array for charachters representing log severity in the log file */
171 static const char arr_loglevel_chars[] = {'S', 'E', 'W', 'N', 'I', 'D'};
172 static const int num_loglevel_chars = COUNT_ARRAY(arr_loglevel_chars);
173
174 #else /* #ifndef DISABLE_LOGGER */
175   char *disabled_logger_processname=NULL;
176 #endif /* DISABLE_LOGGER */
177 /* ==========================================================================
178     Global function definitions
179    ========================================================================== */
180
181 #ifndef DISABLE_LOGGER
182
183 /* remember I'm keeping a copy of the actual char * Filename, so you mustn't
184    delete it, until you've finished with the log.  Also you're responsible
185    for deleting it when you have finished with it. */
186 void log_init()
187 {
188   if (global_log_data.logs==NULL)
189   {
190     /* first check default_log_file_data_pair */
191
192     /* next clear out the log_file_data_array */
193     memset(log_file_data_array, 0, sizeof(log_file_data_array));
194     /* now set default_log_file_data_pairs */
195     log_file_data_array[0] = &default_log_file_data_pair;
196
197     /* now setup the global_log_data struct */
198     global_log_data.logs = log_file_data_array;
199   }
200 }
201 #endif /* #ifndef DISABLE_LOGGER */
202
203 bool log_setup(char *filename, enum loglevels loglevel, enum logtypes logtype, int display_options)
204 {
205 #ifndef DISABLE_LOGGER
206
207   struct stat statbuf;
208   int firstattempt;
209   int retval;
210   gid_t gid;
211   uid_t uid;
212   int access;
213   char lastchar[2];
214
215   log_file_data_pair *logs;
216
217   log_init();
218
219   logs = log_file_arr[logtype];
220
221   if (logs==NULL)
222   {
223     logs = (log_file_data_pair *)malloc(sizeof(log_file_data_pair));
224     if (logs==NULL)
225     {
226       LOG(log_severe, logtype_logger, "can't calloc in log_setup");
227     }
228     else
229     {
230       /* memcpy(logs, log_file_arr[logtype_default], sizeof(log_file_data_pair)); */
231       log_file_arr[logtype] = logs;
232       (*logs)[1].log_file = NULL;
233     }
234   }
235
236   if ( ((*logs)[1].log_file == stdout) && ((*logs)[1].log_file != NULL) )
237   {
238     fclose((*logs)[1].log_file);
239     (*logs)[1].log_file = NULL;
240   }
241
242   if (strlen(global_log_data.log_file_directory)>0)
243   {
244     lastchar[0] = global_log_data.log_file_directory[strlen(global_log_data.log_file_directory)-1];
245
246     if (lastchar[0] == '/' || lastchar[0] == '\\' || lastchar[0] == ':')
247       lastchar[0] = 0;
248     else
249       /* this should probably be a platform specific path separator */
250       lastchar[0] = '/';
251
252     lastchar[1] = 0;
253   }
254   else
255     lastchar[0] = 0;
256   
257 #ifdef DEBUG_OUTPUT_TO_SCREEN
258   printf("filename is %s stored at location %p\n", (*logs)[1].log_filename, 
259                                                    (*logs)[1].log_filename);
260 #endif /* DEBUG_OUTPUT_TO_SCREEN */
261   if (filename == NULL)
262   {
263     strncpy((*logs)[1].log_filename, (*(log_file_arr[0]))[1].log_filename, PATH_MAX);
264   }
265   else
266   {
267     sprintf((*logs)[1].log_filename, "%s%s%s", global_log_data.log_file_directory,
268                                          lastchar, filename);
269   }
270   (*logs)[1].log_level    = loglevel;
271   (*logs)[1].display_options = display_options;
272
273 #ifdef DEBUG_OUTPUT_TO_SCREEN
274   printf("filename is %s stored at location %p\n", (*logs)[1].log_filename, 
275                                                    (*logs)[1].log_filename);
276 #endif /* DEBUG_OUTPUT_TO_SCREEN */
277
278 #ifdef CHECK_STAT_ON_NEW_FILES
279   uid = geteuid(); 
280   gid = getegid();
281   
282 #ifdef DEBUG_OUTPUT_TO_SCREEN
283   printf("about to stat file %s\n", (*logs)[1].log_filename);
284 #endif
285   firstattempt = stat((*logs)[1].log_filename, &statbuf);
286
287   if (firstattempt == -1)
288   {
289 #ifdef DEBUG_OUTPUT_TO_SCREEN
290     printf("about to call Log with %d, %d, %s, %s\n", log_note, logtype_logger, 
291            "can't stat Logfile",    (*logs)[1].log_filename);
292 #endif
293
294     /* syslog(LOG_INFO, "stat failed"); */
295     LOG(log_warning, logtype_logger, "stat fails on file %s",
296                      (*logs)[1].log_filename);
297
298     if (strlen(global_log_data.log_file_directory)>0)
299     {
300       retval = stat(global_log_data.log_file_directory, &statbuf);
301       if (retval == -1)
302       {
303 #ifdef DEBUG_OUTPUT_TO_SCREEN
304         printf("can't stat dir either so I'm giving up\n");
305 #endif
306         LOG(log_severe, logtype_logger, "can't stat directory %s either",
307                          global_log_data.log_file_directory);
308         return false;
309       } 
310     }
311   }
312
313 #ifdef CHECK_ACCESS_ON_NEW_FILES
314   access = ((statbuf.st_uid == uid)?(statbuf.st_mode & S_IWUSR):0) + 
315            ((statbuf.st_gid == gid)?(statbuf.st_mode & S_IWGRP):0) +
316            (statbuf.st_mode & S_IWOTH);
317
318   if (access==0)
319   {
320 #ifdef DEBUG_OUTPUT_TO_SCREEN
321     printf("failing with %d, %d, %s, %s\n", log_note, logtype_logger,
322            "can't access Logfile %s",    (*logs)[1].log_filename);
323 #endif
324
325     LOG(log_note, logtype_logger, "can't access file %s", 
326                      (*logs)[1].log_filename);
327     return false;
328   }
329 #endif /* CHECK_ACCESS_ON_NEW_FILES */
330 #endif /* CHECK_STAT_ON_NEW_FILES */
331 #ifdef KEEP_LOGFILES_OPEN
332   if ((*logs)[1].log_file!=NULL)
333     fclose((*logs)[1].log_file);
334
335   (*logs)[1].log_file     = fopen((*logs)[1].log_filename, "at");
336   if ((*logs)[1].log_file == NULL)
337   {
338     LOG(log_severe, logtype_logger, "can't open Logfile %s", (*logs)[1].log_filename);
339     return false;
340   }
341 #endif
342
343   LOG(log_info, logtype_logger, "Log setup complete");
344
345 #endif /* DISABLE_LOGGER */
346   return true;
347 }
348
349
350 void syslog_setup(enum loglevels loglevel, enum logtypes logtype, int display_options, int facility)
351 {
352 #ifndef DISABLE_LOGGER
353   log_file_data_pair *logs;
354
355   log_init();
356
357   logs = log_file_arr[logtype];
358
359   if (logs==NULL)
360   {
361     logs = (log_file_data_pair *)malloc(sizeof(log_file_data_pair));    
362     if (logs==NULL)
363     {
364       LOG(log_severe, logtype_logger, "can't calloc in log_setup");
365     }
366     else
367     {
368       memcpy(logs, log_file_arr[logtype_default], sizeof(log_file_data_pair));
369       log_file_arr[logtype] = logs;
370     }
371   }
372
373   (*logs)[0].log_file        = NULL;
374   (*logs)[0].log_filename[0] = 0;
375   (*logs)[0].log_level       = loglevel;
376   (*logs)[0].display_options = display_options;
377
378   openlog(global_log_data.processname, (*logs)[0].display_options, facility);
379
380   LOG(log_info, logtype_logger, "SysLog setup complete");
381 #else /* DISABLE_LOGGER */
382 /* behave like a normal openlog call */
383   openlog(disabled_logger_processname, display_options, facility);
384 #endif /* DISABLE_LOGGER */
385 }
386
387 void log_close()
388 {
389 #ifndef DISABLE_LOGGER
390   log_file_data_pair *logs;
391   int n;
392
393   LOG(log_info, logtype_logger, "Closing logs");
394
395   for(n=(sizeof(log_file_arr)-1);n>0;n--)
396   {
397     logs = log_file_arr[n];
398 #ifdef KEEP_LOGFILES_OPEN
399     if ((*logs)[1].log_file!=NULL)
400       fclose((*logs)[1].log_file);
401 #endif /* KEEP_LOGFILES_OPEN */
402     if (logs!=NULL)
403     {
404 #ifdef DEBUG_OUTPUT_TO_SCREEN
405       printf("Freeing log_data %d, stored at %p\n", n, logs);
406       printf("\t(filename) %s\t(type) %s\n", (*logs)[1].log_filename, 
407              ((n<num_logtype_strings)?arr_logtype_strings[n]:""));
408 #endif /* DEBUG_OUTPUT_TO_SCREEN */
409       free(logs);
410     }
411     log_file_arr[n] = NULL;
412   }
413 #ifdef DEBUG_OUTPUT_TO_SCREEN
414       printf("Freeing log_data %d, stored at %p\n", n, log_file_arr[n]);
415       printf("\t(filename) %s\t(type) %s\n", (*(log_file_arr[n]))[1].log_filename, 
416              ((n<num_logtype_strings)?arr_logtype_strings[n]:""));
417 #endif /* DEBUG_OUTPUT_TO_SCREEN */
418 #endif /* DISABLE_LOGGER */
419
420   closelog();
421 }
422
423 /* This function sets up the processname */
424 void set_processname(char *processname)
425 {
426 #ifndef DISABLE_LOGGER
427   /* strncpy(global_log_data.processname, GetCommandName(processname), 15); */
428   strncpy(global_log_data.processname, processname, 15);
429   global_log_data.processname[15] = 0;
430 #else /* DISABLE_LOGGER */
431   disabled_logger_processname = processname;
432 #endif /* DISABLE_LOGGER */
433 }
434
435 #ifndef DISABLE_LOGGER
436 /* This is called by the macro so set the location of the caller of Log */
437 make_log_func set_log_location(char *srcfilename, int srclinenumber)
438 {
439 #ifdef DEBUG_OUTPUT_TO_SCREEN
440   printf("Setting Log Location\n");
441 #endif
442   global_log_data.temp_src_filename = srcfilename;
443   global_log_data.temp_src_linenumber = srclinenumber;
444
445   return make_log_entry;
446 }
447 #endif /* DISABLE_LOGGER */
448
449 /* --------------------------------------------------------------------------
450     MakeLog has 1 main flaws:
451       The message in its entirity, must fit into the tempbuffer.  
452       So it must be shorter than MAXLOGSIZE
453    -------------------------------------------------------------------------- */
454 void make_log_entry(enum loglevels loglevel, enum logtypes logtype, char *message, ...)
455 {
456   va_list args;
457   char log_buffer[MAXLOGSIZE];
458 #ifndef DISABLE_LOGGER
459   char log_details_buffer[MAXLOGSIZE];
460   bool message_overran_log_buffer = false;
461
462   log_file_data_pair *logs;
463
464   log_init();
465
466   logs = log_file_arr[logtype];
467
468   if (logs==NULL)
469   {
470     logs = log_file_arr[logtype_default];
471   }
472 #ifdef DEBUG_OUTPUT_TO_SCREEN
473   printf("Making Log\n");
474 #endif
475   
476 #endif /* DISABLE_LOGGER */
477
478   /* Initialise the Messages */
479   va_start(args, message);
480
481   vsnprintf(log_buffer, sizeof(log_buffer), message, args);
482
483   /* Finished with args for now */
484   va_end(args);
485
486 #ifdef DISABLE_LOGGER
487   syslog(get_syslog_equivalent(loglevel), "%s", log_buffer);
488 #else /* DISABLE_LOGGER */
489
490 #ifdef DO_SYSLOG
491   /* check if sysloglevel is high enough */
492   if ((*logs)[0].log_level>=loglevel)
493   {
494     int sysloglevel = get_syslog_equivalent(loglevel);
495
496     generate_message_details(log_details_buffer, sizeof(log_details_buffer),
497                               &(*logs)[0], loglevel, logtype);
498
499 #ifdef DEBUG_OUTPUT_TO_SCREEN
500     printf("About to log %s %s\n", log_details_buffer, log_buffer);
501     printf("about to do syslog\n");
502     printf("done onw syslog\n");
503 #endif
504     syslog(sysloglevel, "%s: %s", log_details_buffer, log_buffer);
505     /* syslog(sysloglevel, "%s:%s: %s", log_levelString, log_typeString, LogBuffer); */
506   }
507 #endif
508
509 #ifdef DO_FILELOG
510 #ifdef DEBUG_OUTPUT_TO_SCREEN
511   printf("about to do the filelog\n");
512 #endif
513   /* check if log_level is high enough */
514   if ((*logs)[1].log_level>=loglevel) {    
515  
516 #ifdef DEBUG_OUTPUT_TO_SCREEN
517     printf("Open the Log, FILE* is %p\n", (*logs)[1].log_file);
518 #endif
519     /* if log isn't open, open it */
520     if ((*logs)[1].log_file==NULL) {
521 #ifdef DEBUG_OUTPUT_TO_SCREEN
522       printf("Opening the Log, filename is %s\n", (*logs)[1].log_filename);
523 #endif
524       (*logs)[1].log_file     = fopen((*logs)[1].log_filename, "at");
525       if ((*logs)[1].log_file == NULL)
526       {
527         (*logs)[1].log_file = stdout;
528         LOG(log_severe, logtype_logger, "can't open Logfile %s", (*logs)[1].log_filename);
529         return;
530       }
531     }
532     generate_message_details(log_details_buffer, sizeof(log_details_buffer),
533                              &(*logs)[1], loglevel, logtype);
534
535 #ifdef DEBUG_OUTPUT_TO_SCREEN
536     printf("Files open, lets log\n");
537     printf("FILE* is %p\n", (*logs)[1].log_file);
538     printf("%s: %s\n", log_details_buffer, log_buffer);
539 #endif
540
541     fprintf((*logs)[1].log_file, "%s: %s\n", log_details_buffer, log_buffer);
542
543 #ifndef KEEP_LOGFILES_OPEN
544     if ((*logs)[1].log_file != stdout)
545     {
546 #ifdef DEBUG_OUTPUT_TO_SCREEN
547       printf("Closing %s\n", (*logs)[1].log_filename);
548 #endif
549       fclose((*logs)[1].log_file);
550       (*logs)[1].log_file = NULL;
551 #ifdef DEBUG_OUTPUT_TO_SCREEN
552       printf("Closed\n");
553 #endif
554     }
555 #endif 
556   }
557 #endif
558
559   global_log_data.temp_src_filename = NULL;
560   global_log_data.temp_src_linenumber = 0;
561 #endif /* DISABLE_LOGGER */
562 }
563
564 #ifndef DISABLE_LOGGER
565 void load_proccessname_from_proc()
566 {
567   pid_t pid = getpid();
568   char buffer[PATH_MAX];
569   char procname[16];
570   FILE * statfile;
571   char *ptr;
572
573   sprintf(buffer, "/proc/%d/stat", pid);
574   statfile = fopen(buffer, "rt");
575   fgets(buffer, PATH_MAX-1, statfile);
576   fclose(statfile);
577
578   ptr = (char *)strrchr(buffer, ')');
579   *ptr = '\0';
580   memset(procname, 0, sizeof procname);
581   sscanf(buffer, "%d (%15c", &pid, procname);   /* comm[16] in kernel */
582
583   set_processname(procname);
584 }
585
586 /* ==========================================================================
587     Internal function definitions
588    ========================================================================== */
589
590 static char *get_command_name(char *commandpath)
591 {
592   char *ptr;
593 #ifdef DEBUG_OUTPUT_TO_SCREEN
594   printf("getting command name %s\n",commandpath);
595 #endif
596   ptr = (char *)strrchr(commandpath, '/');
597   if (ptr==NULL)
598     ptr = commandpath;
599   else
600     ptr++;
601
602 #ifdef DEBUG_OUTPUT_TO_SCREEN
603   printf("Concluded %s\n", ptr);
604 #endif
605   return ptr;
606 }
607
608 void  workout_what_to_print(struct what_to_print_array *what_to_print, struct tag_log_file_data *log_struct)
609 {
610   /* is this a syslog entry? */
611   if (log_struct->log_filename[0]==NULL)
612   {
613     what_to_print->print_datetime = false;
614     what_to_print->print_processname = false;
615     what_to_print->print_pid = false;
616   }
617   else
618   {
619     what_to_print->print_datetime = true;
620     what_to_print->print_processname = true;
621  
622     /* pid is dealt with at the syslog level if we're syslogging */
623     what_to_print->print_pid = (((log_struct->display_options & logoption_pid) == 0)?false:true);
624   }
625
626   what_to_print->print_srcfile = (((log_struct->display_options & logoption_nfile) == 0)?true:false);
627   what_to_print->print_srcline = (((log_struct->display_options & logoption_nline) == 0)?true:false);
628   
629   what_to_print->print_errlevel = true;
630   what_to_print->print_errtype = true;
631 }
632
633 void generate_message_details(char *message_details_buffer, 
634                               int message_details_buffer_length,
635                               struct tag_log_file_data *log_struct,
636                               enum loglevels loglevel, enum logtypes logtype)
637 {
638   char datebuffer[32];
639   char processinfo[64];
640
641   char *ptr = message_details_buffer;
642   int   templen;
643   int   len = message_details_buffer_length;
644
645   char log_buffer[MAXLOGSIZE];
646   const char *logtype_string;
647
648   char loglevel_string[12]; /* max int size is 2 billion, or 10 digits */
649   bool message_overran_log_buffer = false;
650
651   struct what_to_print_array what_to_print;
652
653   workout_what_to_print(&what_to_print, log_struct);
654
655 #ifdef DEBUG_OUTPUT_TO_SCREEN
656   printf("Making MessageDetails\n");
657 #endif
658
659   *ptr = 0;
660   /*
661   datebuffer[0] = 0;
662   ptr = datebuffer;
663   */
664
665   if (what_to_print.print_datetime)
666   {
667     time_t thetime;
668     time(&thetime);
669
670   /* some people might prefer localtime() to gmtime() */
671     strftime(ptr, len, "%b %d %H:%M:%S", gmtime(&thetime));
672 #ifdef DEBUG_OUTPUT_TO_SCREEN
673     printf("date is %s\n", ptr);
674 #endif
675
676     templen = strlen(ptr);
677     len -= templen;
678     if (what_to_print.print_processname || what_to_print.print_pid)
679       strncat(ptr, " ", len);
680     else
681       strncat(ptr, ":", len);
682
683     templen++;
684     len --;
685     ptr += templen;
686   }
687
688   /*
689   processinfo[0] = 0;
690   ptr = processinfo;
691   */
692
693   if (what_to_print.print_processname)
694   {
695     strncpy(ptr, global_log_data.processname, len);
696
697     templen = strlen(ptr);
698     len -= templen;
699     ptr += templen;
700   }
701
702   if (what_to_print.print_pid)
703   {
704     pid_t pid = getpid();
705
706     sprintf(ptr, "[%d]", pid);
707
708     templen = strlen(ptr);
709     len -= templen;
710     ptr += templen;
711   }
712
713   if (what_to_print.print_srcfile || what_to_print.print_srcline)
714   {
715     char sprintf_buffer[8];
716     char *buff_ptr;
717
718     sprintf_buffer[0] = '[';
719     if (what_to_print.print_srcfile)
720     {
721       strcpy(&sprintf_buffer[1], "%s");
722       buff_ptr = &sprintf_buffer[3];
723     }
724     if (what_to_print.print_srcfile && what_to_print.print_srcline)
725     {
726       strcpy(&sprintf_buffer[3], ":");
727       buff_ptr = &sprintf_buffer[4];
728     }
729     if (what_to_print.print_srcline)
730     {
731       strcpy(buff_ptr, "%d");
732       buff_ptr = &buff_ptr[2];
733     }
734     strcpy(buff_ptr, "]");
735
736     /* ok sprintf string is ready, now is the 1st parameter src or linenumber */
737     if (what_to_print.print_srcfile)
738     {
739       sprintf(ptr, sprintf_buffer, 
740              global_log_data.temp_src_filename, global_log_data.temp_src_linenumber);
741     }
742     else
743     {
744       sprintf(ptr, sprintf_buffer, global_log_data.temp_src_linenumber);
745     }
746
747 #ifdef DEBUG_OUTPUT_TO_SCREEN
748     printf("Process info is %s\n", ptr);
749 #endif
750
751     templen = strlen(ptr);
752     len -= templen;
753     ptr += templen;
754
755   }
756
757   if (what_to_print.print_processname || what_to_print.print_pid ||
758       what_to_print.print_srcfile || what_to_print.print_srcline)
759   {
760     strncat(ptr, ": ", len);
761     len -= 2;
762     ptr += 2;
763   }
764
765 /*
766   loglevel_string[0] = 0;
767   ptr = loglevel_string;
768 */
769
770   if (what_to_print.print_errlevel)
771   {
772     if ((loglevel/10) >= (num_loglevel_chars-1))
773     {
774       sprintf(ptr, "%c%d", arr_loglevel_chars[num_loglevel_chars-1],
775                                        loglevel/10);
776     }
777     else
778     {
779       sprintf(ptr, "%c", arr_loglevel_chars[loglevel/10]);
780     }
781
782     templen = strlen(ptr);
783     len -= templen;
784     ptr += templen;    
785   }
786
787   if (what_to_print.print_errtype)
788   {
789     const char *logtype_string;
790
791     /* get string represnetation of the Log Type */
792     if (logtype<num_logtype_strings)
793       logtype_string = arr_logtype_strings[logtype];
794     else
795       logtype_string = "";
796
797     if (what_to_print.print_errlevel)
798     {
799       strncat(ptr, ":", len);
800       ptr++;
801     }
802
803     sprintf(ptr, "%s", logtype_string);
804   }
805
806   message_details_buffer[message_details_buffer_length-1] = 0;
807
808 #ifdef DEBUG_OUTPUT_TO_SCREEN
809     printf("Message Details are %s\n", message_details_buffer);
810 #endif
811 }
812 #endif /* DISABLE_LOGGER */
813
814 int get_syslog_equivalent(enum loglevels loglevel)
815 {
816   switch (loglevel/10)
817   {
818     /* The question is we know how bad it is for us,
819                     but how should that translate in the syslogs?  */
820     case 0: /* severe */
821       return LOG_ERR;
822     case 1: /* error */
823       return LOG_ERR;
824     case 2: /* warning */
825       return LOG_WARNING;
826     case 3: /* note */
827       return LOG_NOTICE;
828     case 4: /* information */
829       return LOG_INFO;
830     default: /* debug */
831       return LOG_DEBUG;
832   }
833 }
834
835
836
837
838
839
840
841
842