GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: tmp_project/StringUtils/TESTS/TEST_PLOG/main.cpp Lines: 124 124 100.0 %
Date: 2024-12-09 15:30:52 Branches: 279 279 100.0 %

Line Branch Exec Source
1
2
/***************************************
3
	Auteur : Pierre Aubert
4
	Mail : pierre.aubert@lapp.in2p3.fr
5
	Licence : CeCILL-C
6
****************************************/
7
8
#include <time.h>
9
#include <iostream>
10
#include "phoenix_assert.h"
11
#include "PLog.h"
12
13
///Test the conversion of log level into string
14
1
void testLogLevelToString(){
15

1
	phoenix_assert(phoenix_logLevelToStr(PLog::DEBUG) == "DEBUG");
16

1
	phoenix_assert(phoenix_logLevelToStr(PLog::INFO) == "INFO");
17

1
	phoenix_assert(phoenix_logLevelToStr(PLog::WARNING) == "WARNING");
18

1
	phoenix_assert(phoenix_logLevelToStr(PLog::ERROR) == "ERROR");
19

1
	phoenix_assert(phoenix_logLevelToStr(PLog::CRITICAL) == "CRITICAL");
20

1
	phoenix_assert(phoenix_logLevelToStr(PLog::ALWAYS) == "ALWAYS");
21
1
}
22
23
///Test the conversion of string into log level
24
1
void testStringToLogLevel(){
25

1
	phoenix_assert(phoenix_strToLogLevel("DEBUG") == PLog::DEBUG);
26

1
	phoenix_assert(phoenix_strToLogLevel("INFO") == PLog::INFO);
27

1
	phoenix_assert(phoenix_strToLogLevel("WARNING") == PLog::WARNING);
28

1
	phoenix_assert(phoenix_strToLogLevel("ERROR") == PLog::ERROR);
29

1
	phoenix_assert(phoenix_strToLogLevel("CRITICAL") == PLog::CRITICAL);
30

1
	phoenix_assert(phoenix_strToLogLevel("ALWAYS") == PLog::ALWAYS);
31
1
}
32
33
///Test the PLog
34
1
void testStringPLog(){
35
2
	PLog log;
36
1
	log.setFileName("logFile.log");
37
1
	log.setThreadIndex(2lu);
38

1
	phoenix_assert(log.open());
39

1
	phoenix_assert(log.getThreadIndex() == 2lu);
40

1
	phoenix_assert(log.getFileName() == "logFile.log");
41
42
1
	log.getLog() << "Some log entry" << std::endl;
43
1
	log.getLog() << "Some other log entry" << std::endl;
44
1
}
45
46
///Test the PLog
47
1
void testStringPLogLevel(){
48
2
	PLog log;
49
1
	log.setFileName("logLevelFile.log");
50
1
	log.setLogLevel(PLog::INFO);
51

1
	phoenix_assert(log.open());
52

1
	phoenix_assert(log.getFileName() == "logLevelFile.log");
53
1
	log.getLog(PLog::DEBUG) << "Some debug log entry" << std::endl;
54
1
	log.getLogDebug() << "Some other debug log entry" << std::endl;
55
1
	log.getLog(PLog::INFO) << "Some info log entry" << std::endl;
56
1
	log.getLog() << "Some other info log entry" << std::endl;
57
1
	log.getLogInfo() << "Some other info log entry (again)" << std::endl;
58
1
	log.getLog(PLog::WARNING) << "Some warning log entry" << std::endl;
59
1
	log.getLogWarning() << "Some other warning log entry" << std::endl;
60
1
	log.getLog(PLog::ERROR) << "Some error log entry" << std::endl;
61
1
	log.getLogError() << "Some other error log entry" << std::endl;
62
1
	log.getLog(PLog::CRITICAL) << "Some critical log entry" << std::endl;
63
1
	log.getLogCritical() << "Some other critical log entry" << std::endl;
64
1
}
65
66
///Test the PLog
67
1
void testStringMultiPLog(){
68
2
	PLog log;
69
1
	log.setFileName("logMultiFile.log");	//Always set the file name first
70
1
	size_t nbThread(4lu);
71
1
	log.resize(nbThread);			//Then, set the number of threads
72

1
	phoenix_assert(log.open());			//Then, open all log file
73
1
	log.getLog() << "Log of all threads" << std::endl;
74
5
	for(size_t i(0lu); i < nbThread; ++i){
75
4
		PLog & subLog = log.getLog(i);
76

4
		subLog.getLog() << " i = "<<i<<", Some log entry" << std::endl;
77

4
		subLog.getLog() << " i = "<<i<<", Some other log entry" << std::endl;
78
	}
79
1
}
80
81
///Test if the std::cout redirection is working
82
1
void testLogCoutDedirectInFile(){
83
2
	PLog log;
84
1
	log.setFileName("logFileRedirectCoutCerr.log");
85
1
	log.setMode(PLog::FILE_CAPTURE_STDOUT_STDERR);
86

1
	phoenix_assert(log.open());
87
88
1
	std::cout << "Some redirected std::cout stuff" << std::endl;
89
1
	std::cerr << "Some redirected std::cerr stuff" << std::endl;
90
1
	log.getLog() << "Some classic log" << std::endl;
91
92
1
	log.close();	//Stops the redirection
93
1
}
94
95
///Test if the std::cout redirection is working
96
1
void testLogStdoutOnly(){
97
2
	PLog log;
98
1
	log.setFileName("logFileStdOutOnly.log");
99
1
	log.setMode(PLog::STDOUT_ONLY);
100

1
	phoenix_assert(log.open());
101
102
1
	log.getLog() << "Some log message" << std::endl;
103
1
	log.getLog() << "Some classic log" << std::endl;
104
105
1
	log.close();	//Stops the redirection
106
1
}
107
108
///Test if the std::cout redirection is working
109
1
void testLogDisable(){
110
2
	PLog log;
111
1
	log.setFileName("logFileDisable.log");
112
1
	log.setMode(PLog::DISABLE);
113

1
	phoenix_assert(log.open());
114
1
	log.getLog() << "Some log message" << std::endl;
115
1
	log.getLog() << "Some classic log" << std::endl;
116
1
	log.close();	//Stops the redirection
117
1
}
118
119
///Test if the STRING_ONLY log mode works
120
1
void testStringLogAppend(){
121
2
	PLog log;
122
1
	log.setFileName("logFileStringAppend.log");
123
1
	log.setMode(PLog::FILE_ONLY);
124

1
	phoenix_assert(log.open());
125
1
	log.getLogInfo() << "Let's test log append" << std::endl;
126
127
2
	PLog logStr;
128
1
	logStr.setThreadIndex(1lu);
129
1
	logStr.setLogLevel(PLog::DEBUG);
130
1
	logStr.setMode(PLog::STRING_ONLY);
131

1
	phoenix_assert(logStr.open());
132
133
1
	logStr.getLog(PLog::DEBUG) << "Some debug log entry" << std::endl;
134
1
	logStr.getLogDebug() << "Some other debug log entry" << std::endl;
135
1
	logStr.getLog(PLog::INFO) << "Some info log entry" << std::endl;
136
1
	logStr.getLog() << "Some other info log entry" << std::endl;
137
1
	logStr.getLogInfo() << "Some other info log entry (again)" << std::endl;
138
139
1
	log.getLogInfo() << "Some log info at the same time (should be before log append)" << std::endl;
140
141
1
	logStr.getLog(PLog::WARNING) << "Some warning log entry" << std::endl;
142
1
	logStr.getLogWarning() << "Some other warning log entry" << std::endl;
143
1
	logStr.getLog(PLog::ERROR) << "Some error log entry" << std::endl;
144
1
	logStr.getLogError() << "Some other error log entry" << std::endl;
145
1
	logStr.getLog(PLog::CRITICAL) << "Some critical log entry" << std::endl;
146
1
	logStr.getLogCritical() << "Some other critical log entry" << std::endl;
147
148
1
	log.getLogInfo() << "Just before log append" << std::endl;
149
// 	logStr.close();
150
1
	log.appendLog(logStr.getLogString());
151
1
	logStr.close();
152
1
	log.getLogInfo() << "Just after log append" << std::endl;
153
154
1
	log.close();
155
1
}
156
157
1
int main(int argc, char** argv){
158
1
	testLogLevelToString();
159
1
	testStringToLogLevel();
160
1
	testStringPLog();
161
1
	testStringPLogLevel();
162
1
	testStringMultiPLog();
163
1
	testLogCoutDedirectInFile();
164
1
	testLogStdoutOnly();
165
1
	testLogDisable();
166
1
	testStringLogAppend();
167
1
	return 0;
168
}
169
170