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