Brian Silverman | f7bd1c2 | 2015-12-24 16:07:11 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2015. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "Log.h" |
| 9 | |
| 10 | #include <cstdio> |
| 11 | #ifdef _WIN32 |
| 12 | #include <cstdlib> |
| 13 | #else |
| 14 | #include <cstring> |
| 15 | #endif |
| 16 | |
| 17 | #ifdef __APPLE__ |
| 18 | #include <libgen.h> |
| 19 | #endif |
| 20 | |
| 21 | using namespace nt; |
| 22 | |
| 23 | ATOMIC_STATIC_INIT(Logger) |
| 24 | |
| 25 | static void def_log_func(unsigned int level, const char* file, |
| 26 | unsigned int line, const char* msg) { |
| 27 | if (level == 20) { |
| 28 | std::fprintf(stderr, "NT: %s\n", msg); |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | const char* levelmsg; |
| 33 | if (level >= 50) |
| 34 | levelmsg = "CRITICAL"; |
| 35 | else if (level >= 40) |
| 36 | levelmsg = "ERROR"; |
| 37 | else if (level >= 30) |
| 38 | levelmsg = "WARNING"; |
| 39 | else |
| 40 | return; |
| 41 | #ifdef _WIN32 |
| 42 | char fname[60]; |
| 43 | char ext[10]; |
| 44 | _splitpath_s(file, nullptr, 0, nullptr, 0, fname, 60, ext, 10); |
| 45 | std::fprintf(stderr, "NT: %s: %s (%s%s:%d)\n", levelmsg, msg, fname, ext, |
| 46 | line); |
| 47 | #elif __APPLE__ |
| 48 | int len = strlen(msg) + 1; |
| 49 | char* basestr = new char[len + 1]; |
| 50 | strncpy(basestr, file, len); |
| 51 | std::fprintf(stderr, "NT: %s: %s (%s:%d)\n", levelmsg, msg, basename(basestr), |
| 52 | line); |
| 53 | delete[] basestr; |
| 54 | #else |
| 55 | std::fprintf(stderr, "NT: %s: %s (%s:%d)\n", levelmsg, msg, basename(file), |
| 56 | line); |
| 57 | #endif |
| 58 | } |
| 59 | |
| 60 | Logger::Logger() : m_func(def_log_func) {} |
| 61 | |
| 62 | Logger::~Logger() {} |