mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
Add logging implementation.
This commit is contained in:
66
src/Log.cpp
Normal file
66
src/Log.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include "Log.h"
|
||||
|
||||
#include <cstdio>
|
||||
#ifdef _WIN32
|
||||
#include <cstdlib>
|
||||
#else
|
||||
#include <cstring>
|
||||
#endif
|
||||
|
||||
#ifdef __APPLE__
|
||||
#include <libgen.h>
|
||||
#endif
|
||||
|
||||
#ifdef __ANDROID__
|
||||
#include <libgen.h>
|
||||
#endif
|
||||
|
||||
using namespace cs;
|
||||
|
||||
ATOMIC_STATIC_INIT(Logger)
|
||||
|
||||
static void def_log_func(unsigned int level, const char* file,
|
||||
unsigned int line, const char* msg) {
|
||||
if (level == 20) {
|
||||
std::fprintf(stderr, "CS: %s\n", msg);
|
||||
return;
|
||||
}
|
||||
|
||||
const char* levelmsg;
|
||||
if (level >= 50)
|
||||
levelmsg = "CRITICAL";
|
||||
else if (level >= 40)
|
||||
levelmsg = "ERROR";
|
||||
else if (level >= 30)
|
||||
levelmsg = "WARNING";
|
||||
else
|
||||
return;
|
||||
#ifdef _WIN32
|
||||
char fname[60];
|
||||
char ext[10];
|
||||
_splitpath_s(file, nullptr, 0, nullptr, 0, fname, 60, ext, 10);
|
||||
std::fprintf(stderr, "CS: %s: %s (%s%s:%d)\n", levelmsg, msg, fname, ext,
|
||||
line);
|
||||
#elif __APPLE__
|
||||
int len = strlen(msg) + 1;
|
||||
char* basestr = new char[len + 1];
|
||||
strncpy(basestr, file, len);
|
||||
std::fprintf(stderr, "CS: %s: %s (%s:%d)\n", levelmsg, msg, basename(basestr),
|
||||
line);
|
||||
delete[] basestr;
|
||||
#else
|
||||
std::fprintf(stderr, "CS: %s: %s (%s:%d)\n", levelmsg, msg, basename(file),
|
||||
line);
|
||||
#endif
|
||||
}
|
||||
|
||||
Logger::Logger() { SetLogger(def_log_func); }
|
||||
|
||||
Logger::~Logger() {}
|
||||
49
src/Log.h
Normal file
49
src/Log.h
Normal file
@@ -0,0 +1,49 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#ifndef CAMERASERVER_LOG_H_
|
||||
#define CAMERASERVER_LOG_H_
|
||||
|
||||
#include <functional>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
#include "support/atomic_static.h"
|
||||
#include "support/Logger.h"
|
||||
|
||||
namespace cs {
|
||||
|
||||
class Logger : public wpi::Logger {
|
||||
public:
|
||||
static Logger& GetInstance() {
|
||||
ATOMIC_STATIC(Logger, instance);
|
||||
return instance;
|
||||
}
|
||||
~Logger();
|
||||
|
||||
private:
|
||||
Logger();
|
||||
|
||||
ATOMIC_STATIC_DECL(Logger)
|
||||
};
|
||||
|
||||
#define LOG(level, x) WPI_LOG(cs::Logger::GetInstance(), level, x)
|
||||
|
||||
#undef ERROR
|
||||
#define ERROR(x) WPI_ERROR(cs::Logger::GetInstance(), x)
|
||||
#define WARNING(x) WPI_WARNING(cs::Logger::GetInstance(), x)
|
||||
#define INFO(x) WPI_INFO(cs::Logger::GetInstance(), x)
|
||||
|
||||
#define DEBUG(x) WPI_DEBUG(cs::Logger::GetInstance(), x)
|
||||
#define DEBUG1(x) WPI_DEBUG1(cs::Logger::GetInstance(), x)
|
||||
#define DEBUG2(x) WPI_DEBUG2(cs::Logger::GetInstance(), x)
|
||||
#define DEBUG3(x) WPI_DEBUG3(cs::Logger::GetInstance(), x)
|
||||
#define DEBUG4(x) WPI_DEBUG4(cs::Logger::GetInstance(), x)
|
||||
|
||||
} // namespace cs
|
||||
|
||||
#endif // CAMERASERVER_LOG_H_
|
||||
Reference in New Issue
Block a user