1 // Copyright (c) 1999-2018 David Muse
2 // See the file COPYING for more information
3 
4 #include <rudiments/logger.h>
5 #include <rudiments/permissions.h>
6 #include <rudiments/datetime.h>
7 #include <rudiments/snooze.h>
8 #include <rudiments/process.h>
9 #include <rudiments/stringbuffer.h>
10 #ifdef RUDIMENTS_HAVE_SYSLOG_H
11 	#include <syslog.h>
12 #endif
13 #include "test.cpp"
14 
main(int argc,const char ** argv)15 int main(int argc, const char **argv) {
16 
17 	header("logger");
18 
19 	file::remove("test.log");
20 
21 	logger			lg;
22 
23 	// initialize the log destinations
24 	#ifdef RUDIMENTS_HAVE_SYSLOG_H
25 	syslogdestination	sd;
26 	sd.open("logtest",LOG_CONS,LOG_USER,LOG_INFO);
27 	#endif
28 	filedestination		fd;
29 	test("file",fd.open("test.log",
30 			permissions::evalPermString("rw-------")));
31 	stdoutdestination	sod;
32 	stderrdestination	sed;
33 
34 	// add log destinations
35 	#ifdef RUDIMENTS_HAVE_SYSLOG_H
36 	lg.addLogDestination(&sd);
37 	#endif
38 	lg.addLogDestination(&fd);
39 	lg.addLogDestination(&sod);
40 	lg.addLogDestination(&sed);
41 
42 
43 	// some crash tests...
44 
45 	// remove log destnations all at once
46 	lg.removeAllLogDestinations();
47 
48 	// add them back
49 	#ifdef RUDIMENTS_HAVE_SYSLOG_H
50 	lg.addLogDestination(&sd);
51 	#endif
52 	lg.addLogDestination(&fd);
53 	lg.addLogDestination(&sod);
54 	lg.addLogDestination(&sed);
55 
56 	// remove them one by one
57 	#ifdef RUDIMENTS_HAVE_SYSLOG_H
58 	lg.removeLogDestination(&sd);
59 	#endif
60 	lg.removeLogDestination(&fd);
61 	lg.removeLogDestination(&sod);
62 	lg.removeLogDestination(&sed);
63 
64 	// remove them all at once (even though none should currently be added)
65 	lg.removeAllLogDestinations();
66 
67 
68 	// get the current date/time so we can verify the header
69 	// (make sure we're not about to bump over to the next minute)
70 	datetime	dt;
71 	dt.getSystemDateAndTime();
72 	if (dt.getSeconds()>=58) {
73 		snooze::macrosnooze(5);
74 		dt.getSystemDateAndTime();
75 	}
76 
77 	// create and verify the header
78 	char	*header=logger::logHeader("logtest");
79 	test("header month",charstring::toInteger(header)==dt.getMonth());
80 	test("header day",charstring::toInteger(header+3)==dt.getDayOfMonth());
81 	test("header year",charstring::toInteger(header+6)==dt.getYear());
82 	test("header hour",charstring::toInteger(header+11)==dt.getHour());
83 	test("header minute",charstring::toInteger(header+14)==dt.getMinutes());
84 	test("header program",
85 		// (date string may or may not include the timezone)
86 		!charstring::compare(header+24,"logtest ",8) ||
87 		!charstring::compare(header+21,"logtest ",8));
88 	test("header pid",
89 		// (date string may or may not include the timezone)
90 		charstring::toInteger(header+33)==process::getProcessId() ||
91 		charstring::toInteger(header+30)==process::getProcessId());
92 
93 
94 	// write various log messages (even though no destinations exist)
95 	lg.write(header,0,"test");
96 	lg.write(header,0,'t');
97 	lg.write(header,0,(int32_t)12345);
98 	lg.write(header,0,123.45);
99 	lg.write("",0,"test");
100 	lg.write("",0,'t');
101 	lg.write("",0,(int32_t)12345);
102 	lg.write("",0,123.45);
103 	lg.write(NULL,0,"test");
104 	lg.write(NULL,0,'t');
105 	lg.write(NULL,0,(int32_t)12345);
106 	lg.write(NULL,0,123.45);
107 
108 	// re-add log destinations
109 	#ifdef RUDIMENTS_HAVE_SYSLOG_H
110 	lg.addLogDestination(&sd);
111 	#endif
112 	lg.addLogDestination(&fd);
113 	lg.addLogDestination(&sod);
114 	lg.addLogDestination(&sed);
115 
116 	// write various log messages, with and without header
117 	// (now destinations do exist)
118 	lg.write(header,0,"test");
119 	lg.write(header,0,'t');
120 	lg.write(header,0,(int32_t)12345);
121 	lg.write(header,0,123.45);
122 	lg.write("",0,"test");
123 	lg.write("",0,'t');
124 	lg.write("",0,(int32_t)12345);
125 	lg.write("",0,123.45);
126 	lg.write(NULL,0,"test");
127 	lg.write(NULL,0,'t');
128 	lg.write(NULL,0,(int32_t)12345);
129 	lg.write(NULL,0,123.45);
130 
131 	// verify log file contents
132 	stringbuffer	testcontents;
133 	testcontents.append(header)->append(" : test\n");
134 	testcontents.append(header)->append(" : t\n");
135 	testcontents.append(header)->append(" : 12345\n");
136 	testcontents.append(header)->append(" : 123.4500\n");
137 	testcontents.append("test\n");
138 	testcontents.append("t\n");
139 	testcontents.append("12345\n");
140 	testcontents.append("123.4500\n");
141 	testcontents.append("test\n");
142 	testcontents.append("t\n");
143 	testcontents.append("12345\n");
144 	testcontents.append("123.4500\n");
145 	char	*testlog=file::getContents("test.log");
146 	test("contents",!charstring::compare(testcontents.getString(),testlog));
147 	delete[] testlog;
148 
149 	// close the log destinations
150 	#ifdef RUDIMENTS_HAVE_SYSLOG_H
151 	sd.close();
152 	#endif
153 	fd.close();
154 
155 	// clean up
156 	delete[] header;
157 	file::remove("test.log");
158 }
159