-
Notifications
You must be signed in to change notification settings - Fork 0
/
mylog.c
127 lines (117 loc) · 3.08 KB
/
mylog.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
#include "stdio.h"
#include "stdlib.h"
#include "myfiles.h"
#include "limits.h"
#include "myconfload.h"
#include "unistd.h"
#include "time.h"
#include "mywebserver.h"
#include "string.h"
#include "signal.h"
//srvlog is is ment to run as a child
void srvlog()
{
//Set up the timer for the logs
time_t timer;
char eventtime[26];
struct tm* tm_info;
FILE *log = NULL;
//Load the config to find the log file location
int logstatus=0;
printf("logging child born\n");
fflush(stdout);
char buf[1024];
while(1)
{
//Update the time
time(&timer);
tm_info = localtime(&timer);
strftime(eventtime, 26, "%Y:%m:%d %H:%M:%S", tm_info);
memset(buf,0,1024);
g_logfifofd = open(g_logfifo,O_RDONLY);
if(read(g_logfifofd, buf, 1024))
{
//This test is for the compiler
}
else
{
fprintf(stderr,"SOMETHING WENT WRONG!");
}
if(!strcmp(buf,"status_ready"))
{
logstatus = 1;
printf("\n\nLOG: %s \n", buf);
printf ("\n\nlogstatus = %i\n\n",logstatus);
log = loadFile(g_configstruct.log,"a");
if(log==NULL)
{
logstatus = 2;
printf("LOG FILE COULD NOT BE LOADED! SHUTING DOWN!\n");
kill(g_mainpid,SIGTERM);
}
else
{
printf("Logging Started!\n");
fprintf(log,"%s: Server Initialize & Logging Started\n",eventtime);
}
}
else if(!strcmp(buf,"status_reload"))
{
fclose(log);
g_configstruct = loadConf();
log = loadFile(g_configstruct.log,"a");
}
else if(!strcmp(buf,"status_shutdown"))
{
fprintf(log,"%s: Logging Stopped application closing\n",eventtime);
fflush(log);
fclose(log);
close(g_logfifo);
printf("Log file closed\n");
exit(0);
}
else if(logstatus==2)
{
printf("LOG: %s\n",buf);
printf("LOG: DATA SENT TO LOG BUT THERE IS NO LOG!!!\n");
fflush(stdout);
}
else if(logstatus==1)
{
if(checkPrivilege(g_configstruct.log,"w")!=403)
{
printf("LOG: %s\n",buf);
fprintf(log,"%s: %s\n",eventtime,buf);
fflush(log);
}
else
{
logstatus = 2;
printf("LOG NO LONGER WRITABLE! SHUTING DOWN!\n");
kill(g_mainpid,SIGTERM);
}
}
else
{
printf("\nWARNING CONFIG NOT LOADED\n");
printf ("\n\nlogstatus = %i\n\n",logstatus);
}
close(g_logfifofd);
fflush(stdout);
}
}
//Send log is a simple function that allows other functions
//from other proccess to send something to the log file
int sendLog(char *logmsg)
{
g_logfifofd = open(g_logfifo, O_WRONLY);
if(write(g_logfifofd, logmsg, CHAR_MAX))
{
//To stop warning: ignoring return value of 'write'
}
else
{
fprintf(stderr,"something went wrong!\n");
}
return 0;
}