-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support log backends, add a file log backend.
- Loading branch information
Showing
17 changed files
with
600 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
#include "io/log/ColorLogger.h" | ||
|
||
#include <iostream> | ||
|
||
namespace logger { | ||
|
||
ColorConsole::~ColorConsole() { | ||
// nothing to clean up | ||
} | ||
|
||
void ColorConsole::log(const Source & file, int line, Logger::LogLevel level, const std::string & str) { | ||
|
||
std::ostream * os; | ||
switch(level) { | ||
case Logger::Debug: std::cout << "\e[1;36m[D]\e[0;36m", os = &std::cout; break; | ||
case Logger::Info: std::cout << "\e[1;32m[I]\e[0;32m", os = &std::cout; break; | ||
case Logger::Warning: std::cerr << "\e[1;33m[W]\e[0;33m", os = &std::cerr; break; | ||
case Logger::Error: std::cerr << "\e[1;31m[E]\e[0;31m", os = &std::cerr; break; | ||
case Logger::None: ARX_DEAD_CODE(); | ||
} | ||
|
||
(*os) << ' ' << file.name << "\e[m:\e[0;33m" << line << "\e[m" << " " << str << std::endl; | ||
} | ||
|
||
void ColorConsole::flush() { | ||
std::cout.flush(), std::cerr.flush(); | ||
} | ||
|
||
} // namespace logger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
#ifndef ARX_IO_LOG_COLORLOGGER_H | ||
#define ARX_IO_LOG_COLORLOGGER_H | ||
|
||
#include "io/log/LogBackend.h" | ||
|
||
namespace logger { | ||
|
||
/*! | ||
* ConsoleLogger that colors output message using shell color codes. | ||
*/ | ||
class ColorConsole : public Backend { | ||
|
||
public: | ||
|
||
~ColorConsole(); | ||
|
||
void log(const Source & file, int line, Logger::LogLevel level, const std::string & str); | ||
|
||
void flush(); | ||
|
||
}; | ||
|
||
} // namespace logger | ||
|
||
#endif // ARX_IO_LOG_COLORLOGGER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
|
||
#include "io/log/ConsoleLogger.h" | ||
|
||
#include <cstring> | ||
#include <iostream> | ||
|
||
#include "io/log/ColorLogger.h" | ||
|
||
#include "Configure.h" | ||
|
||
namespace logger { | ||
|
||
Console::~Console() { | ||
// nothing to clean up | ||
} | ||
|
||
void Console::log(const Source & file, int line, Logger::LogLevel level, const std::string & str) { | ||
|
||
std::ostream * os; | ||
switch(level) { | ||
case Logger::Debug: std::cout << "[D]", os = &std::cout; break; | ||
case Logger::Info: std::cout << "[I]", os = &std::cout; break; | ||
case Logger::Warning: std::cerr << "[W]", os = &std::cerr; break; | ||
case Logger::Error: std::cerr << "[E]", os = &std::cerr; break; | ||
case Logger::None: ARX_DEAD_CODE(); | ||
} | ||
|
||
(*os) << ' ' << file.name << ':' << line << " " << str << std::endl; | ||
} | ||
|
||
void Console::flush() { | ||
std::cout.flush(), std::cerr.flush(); | ||
} | ||
|
||
static bool is_fd_disabled(int fd) { | ||
|
||
ARX_UNUSED(fd); | ||
|
||
// Disable the console log backend if output is redirected to /dev/null | ||
#ifdef HAVE_READLINK | ||
static const char * names[] = { NULL, "/proc/self/fd/1", "/proc/self/fd/2" }; | ||
char path[64]; | ||
ssize_t len = readlink(names[fd], path, ARRAY_SIZE(path)); | ||
if(len == 9 && !memcmp(names, "/dev/null", 9)) { | ||
return true; | ||
} | ||
#endif | ||
|
||
return false; | ||
} | ||
|
||
Backend * Console::get() { | ||
|
||
#ifdef HAVE_ISATTY | ||
if(isatty(1) && isatty(2)) { | ||
return new ColorConsole; | ||
} | ||
#endif | ||
|
||
if(is_fd_disabled(1) && is_fd_disabled(2)) { | ||
return NULL; | ||
} | ||
|
||
return new Console; | ||
} | ||
|
||
} // namespace logger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
#ifndef ARX_IO_LOG_CONSOLELOGGER_H | ||
#define ARX_IO_LOG_CONSOLELOGGER_H | ||
|
||
#include "io/log/LogBackend.h" | ||
|
||
namespace logger { | ||
|
||
/*! | ||
* Simple logger that prints plain text to standard output. | ||
*/ | ||
class Console : public Backend { | ||
|
||
public: | ||
|
||
~Console(); | ||
|
||
void log(const Source & file, int line, Logger::LogLevel level, const std::string & str); | ||
|
||
void flush(); | ||
|
||
/*! | ||
* @return a ColorLogger instance if colors are supported or a ConsoleLogger otherwise. | ||
* May return null if standard output / error is discarded. | ||
*/ | ||
static Backend * get(); | ||
|
||
}; | ||
|
||
} // namespace logger | ||
|
||
#endif // ARX_IO_LOG_CONSOLELOGGER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
#include "io/log/FileLogger.h" | ||
|
||
#include "io/log/ColorLogger.h" | ||
|
||
#include "Configure.h" | ||
|
||
namespace logger { | ||
|
||
File::~File() { | ||
// nothing to clean up | ||
} | ||
|
||
void File::log(const Source & file, int line, Logger::LogLevel level, const std::string & str) { | ||
|
||
switch(level) { | ||
case Logger::Debug: ofs << "[D]"; break; | ||
case Logger::Info: ofs << "[I]"; break; | ||
case Logger::Warning: ofs << "[W]"; break; | ||
case Logger::Error: ofs << "[E]"; break; | ||
case Logger::None: ARX_DEAD_CODE(); | ||
} | ||
|
||
ofs << ' ' << file.name << ':' << line << " " << str << std::endl; | ||
} | ||
|
||
void File::flush() { | ||
ofs.flush(); | ||
} | ||
|
||
} // namespace logger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
#ifndef ARX_IO_LOG_FILELOGGER_H | ||
#define ARX_IO_LOG_FILELOGGER_H | ||
|
||
#include "io/FileStream.h" | ||
#include "io/log/LogBackend.h" | ||
|
||
namespace logger { | ||
|
||
/*! | ||
* Simple logger that prints plain text to standard output. | ||
*/ | ||
class File : public Backend { | ||
|
||
fs::ofstream ofs; | ||
|
||
public: | ||
|
||
inline File(const fs::path & path, std::ios_base::openmode mode) | ||
: ofs(path, mode) { } | ||
|
||
~File(); | ||
|
||
void log(const Source & file, int line, Logger::LogLevel level, const std::string & str); | ||
|
||
void flush(); | ||
|
||
}; | ||
|
||
} // namespace logger | ||
|
||
#endif // ARX_IO_LOG_FILELOGGER_H |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
|
||
#ifndef ARX_IO_LOG_LOGBACKEND_H | ||
#define ARX_IO_LOG_LOGBACKEND_H | ||
|
||
#include "io/log/Logger.h" | ||
|
||
namespace logger { | ||
|
||
struct Source { | ||
|
||
const char * file; | ||
|
||
std::string name; | ||
|
||
Logger::LogLevel level; | ||
|
||
}; | ||
|
||
class Backend { | ||
|
||
public: | ||
|
||
virtual ~Backend() { } | ||
|
||
virtual void log(const Source & file, int line, Logger::LogLevel level, const std::string & str) = 0; | ||
|
||
virtual void flush() = 0; | ||
|
||
}; | ||
|
||
} // namespace logger | ||
|
||
#endif // ARX_IO_LOG_LOGBACKEND_H |
Oops, something went wrong.