Files
allwpilib/hal/include/Log.h
Tyler Veness 74f9da9da1 artf2612: Added Python script for updating license in source files (#57)
This is a follow-up of 3cd1253. A C++ program was written to automate the license update originally. That program was translated to Python so it can be kept in the repository and run when needed. It has been tested on Windows using the standard Python 3 installation and on Linux.

The original version skipped files that had "//" at the beginning since most were files that should be excluded. The relevant files are now in an exclusion list and the rest are processed normally. The .hpp file extension has been added as well. The script rewrote CompressorJNI.cpp to remove the carriage returns from its line endings.
2016-05-25 20:23:37 -07:00

126 lines
3.2 KiB
C++

/*----------------------------------------------------------------------------*/
/* 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. */
/*----------------------------------------------------------------------------*/
#pragma once
#include <stdio.h>
#include <iomanip>
#include <sstream>
#include <string>
#ifdef _WIN32
#include <Windows.h>
#else
#include <sys/time.h>
#endif
inline std::string NowTime();
enum TLogLevel {
logNONE,
logERROR,
logWARNING,
logINFO,
logDEBUG,
logDEBUG1,
logDEBUG2,
logDEBUG3,
logDEBUG4
};
class Log {
public:
Log();
virtual ~Log();
std::ostringstream& Get(TLogLevel level = logINFO);
public:
static TLogLevel& ReportingLevel();
static std::string ToString(TLogLevel level);
static TLogLevel FromString(const std::string& level);
protected:
std::ostringstream os;
private:
Log(const Log&);
Log& operator=(const Log&);
};
inline Log::Log() {}
inline std::ostringstream& Log::Get(TLogLevel level) {
os << "- " << NowTime();
os << " " << ToString(level) << ": ";
os << std::string(level > logDEBUG ? level - logDEBUG : 0, '\t');
return os;
}
inline Log::~Log() {
os << std::endl;
fprintf(stderr, "%s", os.str().c_str());
fflush(stderr);
}
inline TLogLevel& Log::ReportingLevel() {
static TLogLevel reportingLevel = logDEBUG4;
return reportingLevel;
}
inline std::string Log::ToString(TLogLevel level) {
static const char* const buffer[] = {"NONE", "ERROR", "WARNING",
"INFO", "DEBUG", "DEBUG1",
"DEBUG2", "DEBUG3", "DEBUG4"};
return buffer[level];
}
inline TLogLevel Log::FromString(const std::string& level) {
if (level == "DEBUG4") return logDEBUG4;
if (level == "DEBUG3") return logDEBUG3;
if (level == "DEBUG2") return logDEBUG2;
if (level == "DEBUG1") return logDEBUG1;
if (level == "DEBUG") return logDEBUG;
if (level == "INFO") return logINFO;
if (level == "WARNING") return logWARNING;
if (level == "ERROR") return logERROR;
if (level == "NONE") return logNONE;
Log().Get(logWARNING) << "Unknown logging level '" << level
<< "'. Using INFO level as default.";
return logINFO;
}
typedef Log FILELog;
#define FILE_LOG(level) \
if (level > FILELog::ReportingLevel()) \
; \
else \
Log().Get(level)
#ifdef _WIN32
inline std::string NowTime() {
SYSTEMTIME st;
GetLocalTime(&st);
char result[100] = {0};
sprintf(result, "%d:%d:%d.%d", st.wHour, st.wMinute, st.wSecond,
st.wMilliseconds);
return result;
}
#else
inline std::string NowTime() {
char buffer[11];
time_t t;
time(&t);
tm* r = gmtime(&t);
strftime(buffer, sizeof(buffer), "%H:%M:%S", r);
struct timeval tv;
gettimeofday(&tv, 0);
char result[100] = {0};
sprintf(result, "%s.%03ld", buffer, (long)tv.tv_usec / 1000);
return result;
}
#endif