File: log.c

package info (click to toggle)
libimager-perl 0.75-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 4,532 kB
  • ctags: 3,278
  • sloc: ansic: 24,109; perl: 21,732; makefile: 13
file content (113 lines) | stat: -rw-r--r-- 2,152 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "imconfig.h"
#include "log.h"
#include <stdlib.h>

#define DTBUFF 50
#define DATABUFF DTBUFF+3+10+1+5+1+1

static int   log_level   = 0;
static FILE *lg_file     = NULL;
static char *date_format = "%Y/%m/%d %H:%M:%S";
static char  date_buffer[DTBUFF];
static char  data_buffer[DATABUFF];


#ifdef IMAGER_LOG

/*
 * Logging is active
 */

void
i_init_log(const char* name,int level) {
  log_level = level;
  if (level < 0) {
    lg_file = NULL;
  } else {
    if (name == NULL) {
      lg_file = stderr;
    } else {
      if (NULL == (lg_file = fopen(name, "w+")) ) { 
	fprintf(stderr,"Cannot open file '%s'\n",name);
	exit(2);
      }
    }
  }
  setvbuf(lg_file, NULL, _IONBF, BUFSIZ);
  mm_log((0,"Imager - log started (level = %d)\n", level));
}

void
i_fatal(int exitcode,const char *fmt, ... ) {
  va_list ap;
  time_t timi;
  struct tm *str_tm;
  
  if (lg_file != NULL) {
    timi = time(NULL);
    str_tm = localtime(&timi);
    if ( strftime(date_buffer, DTBUFF, date_format, str_tm) )
      fprintf(lg_file,"[%s] ",date_buffer);
    va_start(ap,fmt);
    vfprintf(lg_file,fmt,ap);
    va_end(ap);
  }
  exit(exitcode);
}

#else

/*
 * Logging is inactive - insert dummy functions
 */

void i_init_log(const char* name,int onoff) {}
void i_fatal(int exitcode,const char *fmt, ... ) { exit(exitcode); }


#endif

/*
=item i_loog(level, format, ...)
=category Logging

This is an internal function called by the mm_log() macro.

=cut
*/

void
i_loog(int level,const char *fmt, ... ) {
  va_list ap;
  if (level > log_level) return;
  if (lg_file != NULL) {
    fputs(data_buffer, lg_file);
    fprintf(lg_file, "%3d: ",level);
    va_start(ap,fmt);
    vfprintf(lg_file, fmt, ap);
    fflush(lg_file);
    va_end(ap);
  }
}

/*
=item i_lhead(file, line)
=category Logging

This is an internal function called by the mm_log() macro.

=cut
*/

void
i_lhead(const char *file, int line) {
  time_t timi;
  struct tm *str_tm;
  
  if (lg_file != NULL) {
    timi = time(NULL);
    str_tm = localtime(&timi);
    strftime(date_buffer, DTBUFF, date_format, str_tm);
    sprintf(data_buffer, "[%s] %10s:%-5d ", date_buffer, file, line);
  }
}