Kannel: Open Source WAP and SMS gateway  svn-r5335
accesslog.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include <stdarg.h>
#include <string.h>
#include "gwlib.h"

Go to the source code of this file.

Macros

#define FORMAT_SIZE   (10*1024)
 

Functions

void alog_reopen (void)
 
void alog_close (void)
 
void alog_open (char *fname, int use_localtm, int use_markers)
 
void alog_use_localtime (void)
 
void alog_use_gmtime (void)
 
static void format (char *buf, const char *fmt)
 
void alog (const char *fmt,...)
 

Variables

static FILE * file = NULL
 
static char filename [FILENAME_MAX+1]
 
static int use_localtime
 
static int markers = 1
 
static Listwriters = NULL
 

Macro Definition Documentation

◆ FORMAT_SIZE

#define FORMAT_SIZE   (10*1024)

Definition at line 173 of file accesslog.c.

Referenced by alog(), and format().

Function Documentation

◆ alog()

void alog ( const char *  fmt,
  ... 
)

Definition at line 206 of file accesslog.c.

References file, format(), FORMAT_SIZE, gwlist_add_producer(), gwlist_lock(), gwlist_remove_producer(), gwlist_unlock(), and writers.

Referenced by alog_close(), alog_open(), alog_reopen(), bb_alog_sms(), obey_request(), return_reply(), smsbox_req_handle(), and url_result_thread().

207 {
208  char buf[FORMAT_SIZE + 1];
209  va_list args;
210 
211  if (file == NULL)
212  return;
213 
214  format(buf, fmt);
215  va_start(args, fmt);
216 
220 
221  vfprintf(file, buf, args);
222  fflush(file);
223 
225 
226  va_end(args);
227 }
#define FORMAT_SIZE
Definition: accesslog.c:173
static void format(char *buf, const char *fmt)
Definition: accesslog.c:174
void gwlist_unlock(List *list)
Definition: list.c:354
void gwlist_remove_producer(List *list)
Definition: list.c:401
void gwlist_lock(List *list)
Definition: list.c:347
static List * writers
Definition: accesslog.c:83
static FILE * file
Definition: accesslog.c:75
void gwlist_add_producer(List *list)
Definition: list.c:383

◆ alog_close()

void alog_close ( void  )

Definition at line 111 of file accesslog.c.

References alog(), file, gwlist_consume(), gwlist_destroy(), gwlist_lock(), gwlist_unlock(), markers, and writers.

Referenced by alog_open(), and main().

112 {
113 
114  if (file != NULL) {
115  if (markers)
116  alog("Log ends");
118  /* wait for writers to complete */
120  fclose(file);
121  file = NULL;
123  gwlist_destroy(writers, NULL);
124  writers = NULL;
125  }
126 }
static int markers
Definition: accesslog.c:78
void gwlist_unlock(List *list)
Definition: list.c:354
void gwlist_lock(List *list)
Definition: list.c:347
void * gwlist_consume(List *list)
Definition: list.c:427
void alog(const char *fmt,...)
Definition: accesslog.c:206
static List * writers
Definition: accesslog.c:83
static FILE * file
Definition: accesslog.c:75
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)
Definition: list.c:145

◆ alog_open()

void alog_open ( char *  fname,
int  use_localtm,
int  use_markers 
)

Definition at line 129 of file accesslog.c.

References alog(), alog_close(), error(), file, filename, gwlist_create, info(), markers, use_localtime, warning(), and writers.

Referenced by init_bearerbox(), init_smsbox(), and init_wapbox().

130 {
131  FILE *f;
132 
133  use_localtime = use_localtm;
134  markers = use_markers;
135 
136  if (file != NULL) {
137  warning(0, "Opening an already opened access log");
138  alog_close();
139  }
140  if (strlen(fname) > FILENAME_MAX) {
141  error(0, "Access Log filename too long: `%s', cannot open.", fname);
142  return;
143  }
144 
145  if (writers == NULL)
147 
148  f = fopen(fname, "a");
149  if (f == NULL) {
150  error(errno, "Couldn't open logfile `%s'.", fname);
151  return;
152  }
153  file = f;
154  strcpy(filename, fname);
155  info(0, "Started access logfile `%s'.", filename);
156  if (markers)
157  alog("Log begins");
158 }
void error(int err, const char *fmt,...)
Definition: log.c:648
void info(int err, const char *fmt,...)
Definition: log.c:672
static int markers
Definition: accesslog.c:78
static char filename[FILENAME_MAX+1]
Definition: accesslog.c:76
static int use_localtime
Definition: accesslog.c:77
void warning(int err, const char *fmt,...)
Definition: log.c:660
void alog(const char *fmt,...)
Definition: accesslog.c:206
static List * writers
Definition: accesslog.c:83
static FILE * file
Definition: accesslog.c:75
void alog_close(void)
Definition: accesslog.c:111
#define gwlist_create()
Definition: list.h:136

◆ alog_reopen()

void alog_reopen ( void  )

Definition at line 85 of file accesslog.c.

References alog(), error(), file, filename, gwlist_consume(), gwlist_lock(), gwlist_unlock(), markers, and writers.

Referenced by main(), and signal_handler().

86 {
87  if (file == NULL)
88  return;
89 
90  if (markers)
91  alog("Log ends");
92 
94  /* wait for writers to complete */
96 
97  fclose(file);
98  file = fopen(filename, "a");
99 
101 
102  if (file == NULL) {
103  error(errno, "Couldn't re-open access logfile `%s'.", filename);
104  }
105  else if (markers) {
106  alog("Log begins");
107  }
108 }
void error(int err, const char *fmt,...)
Definition: log.c:648
static int markers
Definition: accesslog.c:78
static char filename[FILENAME_MAX+1]
Definition: accesslog.c:76
void gwlist_unlock(List *list)
Definition: list.c:354
void gwlist_lock(List *list)
Definition: list.c:347
void * gwlist_consume(List *list)
Definition: list.c:427
void alog(const char *fmt,...)
Definition: accesslog.c:206
static List * writers
Definition: accesslog.c:83
static FILE * file
Definition: accesslog.c:75

◆ alog_use_gmtime()

void alog_use_gmtime ( void  )

Definition at line 167 of file accesslog.c.

References use_localtime.

168 {
169  use_localtime = 0;
170 }
static int use_localtime
Definition: accesslog.c:77

◆ alog_use_localtime()

void alog_use_localtime ( void  )

Definition at line 161 of file accesslog.c.

References use_localtime.

162 {
163  use_localtime = 1;
164 }
static int use_localtime
Definition: accesslog.c:77

◆ format()

static void format ( char *  buf,
const char *  fmt 
)
static

Definition at line 174 of file accesslog.c.

References FORMAT_SIZE, gw_gmtime(), gw_localtime(), markers, and use_localtime.

Referenced by alog(), and store_status().

175 {
176  time_t t;
177  struct tm tm;
178  char *p, prefix[1024];
179 
180  p = prefix;
181 
182  if (markers) {
183  time(&t);
184  if (use_localtime)
185  tm = gw_localtime(t);
186  else
187  tm = gw_gmtime(t);
188 
189  sprintf(p, "%04d-%02d-%02d %02d:%02d:%02d ",
190  tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
191  tm.tm_hour, tm.tm_min, tm.tm_sec);
192  } else {
193  *p = '\0';
194  }
195 
196  if (strlen(prefix) + strlen(fmt) > FORMAT_SIZE / 2) {
197  sprintf(buf, "%s <OUTPUT message too long>\n", prefix);
198  return;
199  }
200  sprintf(buf, "%s%s\n", prefix, fmt);
201 }
static int markers
Definition: accesslog.c:78
struct tm gw_gmtime(time_t t)
Definition: protected.c:137
#define FORMAT_SIZE
Definition: accesslog.c:173
static int use_localtime
Definition: accesslog.c:77
struct tm gw_localtime(time_t t)
Definition: protected.c:121

Variable Documentation

◆ file

FILE* file = NULL
static

Definition at line 75 of file accesslog.c.

Referenced by alog(), alog_close(), alog_open(), and alog_reopen().

◆ filename

char filename[FILENAME_MAX+1]
static

Definition at line 76 of file accesslog.c.

Referenced by alog_open(), and alog_reopen().

◆ markers

int markers = 1
static

Definition at line 78 of file accesslog.c.

Referenced by alog_close(), alog_open(), alog_reopen(), and format().

◆ use_localtime

int use_localtime
static

Definition at line 77 of file accesslog.c.

Referenced by alog_open(), alog_use_gmtime(), alog_use_localtime(), and format().

◆ writers

List* writers = NULL
static

Definition at line 83 of file accesslog.c.

Referenced by alog(), alog_close(), alog_open(), and alog_reopen().

See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.