-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move driver tests to tests/driver. Add logger and stream settings code.
- Loading branch information
Showing
39 changed files
with
1,017 additions
and
87 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#include "logger.hh" | ||
|
||
#include <iostream> | ||
#include <cstdarg> | ||
#include <chrono> | ||
#include <iomanip> | ||
#include <filesystem> | ||
|
||
LogLevel Logger::current_level = LogLevel_Info; | ||
|
||
void | ||
Logger::set_log_level(LogLevel level) | ||
{ | ||
current_level = level; | ||
} | ||
|
||
LogLevel | ||
Logger::get_log_level() | ||
{ | ||
return current_level; | ||
} | ||
|
||
std::string | ||
Logger::log(LogLevel level, | ||
const char* file, | ||
int line, | ||
const char* func, | ||
const char* format, | ||
...) | ||
{ | ||
if (current_level == LogLevel_None || level < current_level) { | ||
return {}; // Suppress logs | ||
} | ||
|
||
va_list args; | ||
va_start(args, format); | ||
|
||
std::string prefix; | ||
std::ostream* stream = &std::cout; | ||
|
||
switch (level) { | ||
case LogLevel_Debug: | ||
prefix = "[DEBUG] "; | ||
break; | ||
case LogLevel_Info: | ||
prefix = "[INFO] "; | ||
break; | ||
case LogLevel_Warning: | ||
prefix = "[WARNING] "; | ||
stream = &std::cerr; | ||
break; | ||
case LogLevel_Error: | ||
prefix = "[ERROR] "; | ||
stream = &std::cerr; | ||
break; | ||
} | ||
|
||
// Get current time | ||
auto now = std::chrono::system_clock::now(); | ||
auto time = std::chrono::system_clock::to_time_t(now); | ||
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>( | ||
now.time_since_epoch()) % | ||
1000; | ||
|
||
// Get filename without path | ||
std::filesystem::path filepath(file); | ||
std::string filename = filepath.filename().string(); | ||
|
||
// Output timestamp, log level, filename | ||
*stream << std::put_time(std::localtime(&time), "%Y-%m-%d %H:%M:%S") << '.' | ||
<< std::setfill('0') << std::setw(3) << ms.count() << " " << prefix | ||
<< filename << ":" << line << " " << func << ": "; | ||
|
||
char buffer[1024]; | ||
vsnprintf(buffer, sizeof(buffer), format, args); | ||
*stream << buffer << std::endl; | ||
|
||
va_end(args); | ||
|
||
return buffer; | ||
} |
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,38 @@ | ||
#include "zarr.h" | ||
|
||
#include <string> | ||
|
||
class Logger | ||
{ | ||
public: | ||
static void set_log_level(LogLevel level); | ||
static LogLevel get_log_level(); | ||
|
||
static std::string log(LogLevel level, | ||
const char* file, | ||
int line, | ||
const char* func, | ||
const char* format, | ||
...); | ||
|
||
private: | ||
static LogLevel current_level; | ||
}; | ||
|
||
#define LOG_DEBUG(...) \ | ||
Logger::log(LogLevel_Debug, __FILE__, __LINE__, __func__, __VA_ARGS__) | ||
#define LOG_INFO(...) \ | ||
Logger::log(LogLevel_Info, __FILE__, __LINE__, __func__, __VA_ARGS__) | ||
#define LOG_WARNING(...) \ | ||
Logger::log(LogLevel_Warning, __FILE__, __LINE__, __func__, __VA_ARGS__) | ||
#define LOG_ERROR(...) \ | ||
Logger::log(LogLevel_Error, __FILE__, __LINE__, __func__, __VA_ARGS__) | ||
|
||
#define EXPECT(e, ...) \ | ||
do { \ | ||
if (!(e)) { \ | ||
const std::string __err = LOG_ERROR(__VA_ARGS__); \ | ||
throw std::runtime_error(__err); \ | ||
} \ | ||
} while (0) | ||
#define CHECK(e) EXPECT(e, "Expression evaluated as false:\n\t%s", #e) |
Oops, something went wrong.