forked from tedhale/hottub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogfile.c
executable file
·154 lines (114 loc) · 3.02 KB
/
logfile.c
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/***********************************************************************
Filename: logfile.c
antiquity Ted Hale created
************************************************************************/
/* system includes */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#include <string.h>
// needed for the debug flag
#include "hottub.h"
/* global data*/
FILE *logfp;
unsigned long LineNum;
int Julian;
char FileName[100];
/* ------------------------- Log file functions -------------------------- */
/*
CloseLogFile
Closes the current log file if it is in use.
*/
void LogClose(void)
{
fclose(logfp);
logfp = NULL;
}
/*
OpenLogFile
Opens the logfile with the specified name and julian day
Level = level of messages -
0 = errors only, 1 status messages also, 2 = detailed log
RETURNS: 1 if an error occurred creating the file
0 for success
filename should include full path but not suffix
*/
int LogOpen(char *filename)
{
char temp[200];
struct tm *today;
time_t now;
if (filename == NULL) {
return (1);
}
time(&now);
today = localtime(&now);
// save params globally for later
strncpy(FileName, filename, strlen(filename));
// save this in static variable so we knw when day changes
Julian = today->tm_yday+1;
// put together the file name
sprintf(temp,"%s_%04d-%02d-%02d.log",filename,
today->tm_year+1900,today->tm_mon+1,today->tm_mday);
logfp = fopen (temp, "a" );
if ( logfp == NULL ) {
return (1);
}
LineNum = 0;
return (0);
}
/*
Log
Writes a message to the previously opened log file.
Passed in a format string and variable number of parameters,
in the format used by printf.
*/
void Log(char *format, ... )
{
char dtbuf[30];
va_list arglist;
struct tm *today;
time_t now;
time(&now);
today = localtime(&now);
if( Julian != today->tm_yday+1 ) {
LogClose();
LogOpen( FileName );
}
if (logfp==NULL) return;
sprintf(dtbuf,"%02d/%02d/%04d %02d:%02d:%02d",today->tm_mon+1,today->tm_mday,
today->tm_year+1900,today->tm_hour,today->tm_min,today->tm_sec);
fprintf(logfp, "%5lu %s ",LineNum++,dtbuf);
va_start ( arglist, format );
vfprintf (logfp, format, arglist );
//if (format[strlen(format)-1]<' ')
//format[strlen(format)-1] = 0;
fprintf(logfp,"\r\n");
fflush ( logfp );
}
void LogDbg(char *format, ... )
{
char dtbuf[30];
va_list arglist;
struct tm *today;
time_t now;
if (!debug)
return;
time(&now);
today = localtime(&now);
if( Julian != today->tm_yday+1 ) {
LogClose();
LogOpen( FileName );
}
if (logfp==NULL) return;
sprintf(dtbuf,"%02d/%02d/%04d %02d:%02d:%02d",today->tm_mon+1,today->tm_mday,
today->tm_year+1900,today->tm_hour,today->tm_min,today->tm_sec);
fprintf(logfp, "%5lu %s ",LineNum++,dtbuf);
va_start ( arglist, format );
vfprintf (logfp, format, arglist );
//if (format[strlen(format)-1]<' ')
//format[strlen(format)-1] = 0;
fprintf(logfp,"\r\n");
fflush ( logfp );
}