1 |
/* ====================================================================== |
---|
2 |
* Copyright (c) 2000 Theo Schlossnagle |
---|
3 |
* All rights reserved. |
---|
4 |
* The following code was written by Theo Schlossnagle <jesus@omniti.com> |
---|
5 |
* This code was written to facilitate clustered logging via Spread. |
---|
6 |
* More information on Spread can be found at http://www.spread.org/ |
---|
7 |
* Please refer to the LICENSE file before using this software. |
---|
8 |
* ====================================================================== |
---|
9 |
*/ |
---|
10 |
|
---|
11 |
#include <stdio.h> |
---|
12 |
#include <string.h> |
---|
13 |
#include <time.h> |
---|
14 |
#include <sys/time.h> |
---|
15 |
#include <unistd.h> |
---|
16 |
|
---|
17 |
#include "timefuncs.h" |
---|
18 |
|
---|
19 |
#define MAXTIMESTRLEN 128 |
---|
20 |
|
---|
21 |
static char *apmonthnames[12] = |
---|
22 |
{"Jan", "Feb", "Mar", "Apr", "May", "Jun", |
---|
23 |
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; |
---|
24 |
|
---|
25 |
#ifndef MIN |
---|
26 |
#define MIN(a,b) (((a)<(b))?(a):(b)) |
---|
27 |
#endif |
---|
28 |
|
---|
29 |
static char *strnchr(char *string, char chr, int len) { |
---|
30 |
char *cp=string; |
---|
31 |
while(len>0) { |
---|
32 |
if(*cp == chr) return cp; |
---|
33 |
cp++; len--; |
---|
34 |
} |
---|
35 |
return NULL; |
---|
36 |
} |
---|
37 |
void force_local_time(char *string, int *length, const int buffsize, |
---|
38 |
int style, char *format) { |
---|
39 |
char *cp, *cpend, *newcpend; |
---|
40 |
char timebuff[MAXTIMESTRLEN]; |
---|
41 |
int timestrlen, newtimestrlen; |
---|
42 |
int timz; |
---|
43 |
struct tm *t; |
---|
44 |
time_t secs; |
---|
45 |
struct timeval now; |
---|
46 |
struct timezone tz; |
---|
47 |
|
---|
48 |
cp = strnchr(string, '[', *length); |
---|
49 |
if(!cp) return; |
---|
50 |
cpend = strnchr(cp, ']', *length-(cp-string)); |
---|
51 |
if(!cpend) return; |
---|
52 |
cpend++; |
---|
53 |
timestrlen = cpend-cp; |
---|
54 |
gettimeofday(&now, &tz); |
---|
55 |
timz = tz.tz_minuteswest; |
---|
56 |
secs = now.tv_sec; |
---|
57 |
t = localtime(&secs); |
---|
58 |
fprintf(stderr, "Trying to rewrite...\n"); |
---|
59 |
if(style == NO_REWRITE_TIMES) |
---|
60 |
return; |
---|
61 |
else if(style == REWRITE_TIMES_IN_CLF) { |
---|
62 |
char sign = (timz > 0 ? '-' : '+'); |
---|
63 |
timz = (timz < 0)?(-timz):(timz); |
---|
64 |
snprintf(timebuff, sizeof(timebuff), "[%02d/%s/%d:%02d:%02d:%02d %c%.2d%.2d]", |
---|
65 |
t->tm_mday, apmonthnames[t->tm_mon], t->tm_year+1900, |
---|
66 |
t->tm_hour, t->tm_min, t->tm_sec, |
---|
67 |
sign, timz / 60, timz % 60); |
---|
68 |
} else if((style == REWRITE_TIMES_FORMAT) && format) { |
---|
69 |
strftime(timebuff, sizeof(timebuff), format, t); |
---|
70 |
} |
---|
71 |
|
---|
72 |
/* No measure and squeeze it in there */ |
---|
73 |
newtimestrlen = strlen(timebuff); |
---|
74 |
newcpend = cp+newtimestrlen; |
---|
75 |
|
---|
76 |
if(newcpend != cpend) { |
---|
77 |
/* Ugh different size... this is going to me slower */ |
---|
78 |
int chunk = MIN(MIN(string+buffsize-cpend, string+buffsize-newcpend), |
---|
79 |
string+*length-cpend); |
---|
80 |
if(chunk>0) |
---|
81 |
memmove(newcpend, cpend, chunk); |
---|
82 |
*length += newcpend-cpend; |
---|
83 |
if(*length >= buffsize) { |
---|
84 |
newtimestrlen -= *length-buffsize; |
---|
85 |
*length = buffsize; |
---|
86 |
} |
---|
87 |
} |
---|
88 |
memcpy(cp, timebuff, newtimestrlen); |
---|
89 |
return; |
---|
90 |
} |
---|