Skip to content

Commit 4a7afe8

Browse files
committed
Created new ConsoleLoggerService that can be used for debugging on CI servers.
1 parent 6375f36 commit 4a7afe8

File tree

3 files changed

+193
-0
lines changed

3 files changed

+193
-0
lines changed

src/core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ set(SHELLANYTHING_CORE_HEADER_FILES ""
1414
${CMAKE_SOURCE_DIR}/src/core/ConfigFile.h
1515
${CMAKE_SOURCE_DIR}/src/core/ConfigManager.h
1616
${CMAKE_SOURCE_DIR}/src/core/SelectionContext.h
17+
${CMAKE_SOURCE_DIR}/src/core/ConsoleLoggerService.h
1718
${CMAKE_SOURCE_DIR}/src/core/DefaultSettings.h
1819
${CMAKE_SOURCE_DIR}/src/core/Environment.h
1920
${CMAKE_SOURCE_DIR}/src/core/Icon.h
@@ -55,6 +56,7 @@ add_library(sa.core SHARED
5556
ConfigFile.cpp
5657
ConfigManager.cpp
5758
SelectionContext.cpp
59+
ConsoleLoggerService.cpp
5860
DefaultSettings.cpp
5961
FileMagicManager.h
6062
FileMagicManager.cpp

src/core/ConsoleLoggerService.cpp

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/**********************************************************************************
2+
* MIT License
3+
*
4+
* Copyright (c) 2018 Antoine Beauchamp
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*********************************************************************************/
24+
25+
#include "ConsoleLoggerService.h"
26+
27+
#include "rapidassist/timing.h"
28+
#include "rapidassist/filesystem.h"
29+
#include "rapidassist/process.h"
30+
31+
#include <Windows.h> // for GetThreadId
32+
33+
#include <iostream>
34+
35+
#if defined(_WIN32)
36+
#include <chrono>
37+
//typedef struct timeval
38+
//{
39+
// long tv_sec;
40+
// long tv_usec;
41+
//} timeval;
42+
inline int gettimeofday(struct timeval* tp, struct timezone* tzp)
43+
{
44+
namespace sc = std::chrono;
45+
sc::system_clock::duration d = sc::system_clock::now().time_since_epoch();
46+
sc::seconds s = sc::duration_cast<sc::seconds>(d);
47+
tp->tv_sec = (long)s.count();
48+
tp->tv_usec = (long)sc::duration_cast<sc::microseconds>(d - s).count();
49+
50+
return 0;
51+
}
52+
#endif // _WIN32
53+
54+
namespace shellanything
55+
{
56+
57+
inline const char * GetLevelString(const ILoggerService::LOG_LEVEL& level)
58+
{
59+
switch (level)
60+
{
61+
default:
62+
case ILoggerService::LOG_LEVEL_DEBUG:
63+
return "DEBUG";
64+
break;
65+
case ILoggerService::LOG_LEVEL_INFO:
66+
return "INFO";
67+
break;
68+
case ILoggerService::LOG_LEVEL_WARNING:
69+
return "WARNING";
70+
break;
71+
case ILoggerService::LOG_LEVEL_ERROR:
72+
return "ERROR";
73+
break;
74+
case ILoggerService::LOG_LEVEL_FATAL:
75+
return "FATAL";
76+
break;
77+
};
78+
}
79+
80+
inline std::string GetLogTimestamp()
81+
{
82+
struct timeval tv;
83+
time_t nowtime;
84+
struct tm nowtm;
85+
char tmbuf[64], buf[64];
86+
87+
gettimeofday(&tv, NULL);
88+
nowtime = tv.tv_sec;
89+
localtime_s(&nowtm, &nowtime);
90+
strftime(tmbuf, sizeof(tmbuf), "%H:%M:%S", &nowtm); // skip the date format string: %Y-%m-%d
91+
snprintf(buf, sizeof buf, "%s.%06ld", tmbuf, tv.tv_usec);
92+
93+
return buf;
94+
}
95+
96+
ConsoleLoggerService::ConsoleLoggerService()
97+
{
98+
}
99+
100+
ConsoleLoggerService::~ConsoleLoggerService()
101+
{
102+
}
103+
104+
void ConsoleLoggerService::LogMessage(const char* filename, int line, const ILoggerService::LOG_LEVEL& level, const char* message)
105+
{
106+
/*
107+
W0928 09:06:07.668726 29440 ActionProperty.cpp:233] Reporting an error because fail is set to 'true'.
108+
E0928 09:06:14.720094 29440 ConfigManager.cpp:139] Failed searching for configuration files in directory 'D:\Projets\Programmation\Cpp\ShellAnything\ShellAnything\build\bin\Debug\test_workspace\TestPlugins.testProcess'.
109+
*/
110+
111+
const char * level_str = GetLevelString(level);
112+
std::string log_time = GetLogTimestamp();
113+
DWORD dwThreadId = GetThreadId(NULL);
114+
std::string actual_filename = ra::filesystem::GetFilename(filename);
115+
116+
std::cout << level_str << " " << log_time << " " << dwThreadId << " " << actual_filename << ":" << line << "] " << message << "\n";
117+
}
118+
119+
void ConsoleLoggerService::LogMessage(const ILoggerService::LOG_LEVEL& level, const char* message)
120+
{
121+
LogMessage(__FILE__, __LINE__, level, message);
122+
}
123+
124+
} //namespace shellanything

src/core/ConsoleLoggerService.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**********************************************************************************
2+
* MIT License
3+
*
4+
* Copyright (c) 2018 Antoine Beauchamp
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*********************************************************************************/
24+
25+
#ifndef SA_CONSOLE_LOGGER_SERVICE_H
26+
#define SA_CONSOLE_LOGGER_SERVICE_H
27+
28+
#include "ILoggerService.h"
29+
30+
namespace shellanything
31+
{
32+
/// <summary>
33+
/// Abstract logger class.
34+
/// </summary>
35+
class SHELLANYTHING_EXPORT ConsoleLoggerService : public virtual ILoggerService
36+
{
37+
public:
38+
ConsoleLoggerService();
39+
virtual ~ConsoleLoggerService();
40+
41+
private:
42+
// Disable and copy constructor, dtor and copy operator
43+
ConsoleLoggerService(const ConsoleLoggerService&);
44+
ConsoleLoggerService& operator=(const ConsoleLoggerService&);
45+
public:
46+
47+
/// <summary>
48+
/// Send a message to this logger.
49+
/// </summary>
50+
/// <param name="filename">The originating source code file name.</param>
51+
/// <param name="line">The line number that producing this message.</param>
52+
/// <param name="level">The log level of the message.</param>
53+
/// <param name="message">The actual message.</param>
54+
virtual void LogMessage(const char* filename, int line, const ILoggerService::LOG_LEVEL & level, const char* message);
55+
56+
/// <summary>
57+
/// Send a message to this logger.
58+
/// </summary>
59+
/// <param name="level">The log level of the message.</param>
60+
/// <param name="message">The actual message.</param>
61+
virtual void LogMessage(const ILoggerService::LOG_LEVEL & level, const char* message);
62+
63+
};
64+
65+
} //namespace shellanything
66+
67+
#endif //SA_CONSOLE_LOGGER_SERVICE_H

0 commit comments

Comments
 (0)