Skip to content

Commit

Permalink
Formatting and added File Logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ABUCKY0 committed May 13, 2024
1 parent 3f5209c commit 8deecd6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ EXCLUDE_COLD_LIBRARIES:=
# TODO: SET TO 1 FOR LIBRARY
IS_LIBRARY:=1
LIBNAME:=vexlog
VERSION:=0.0.1-alpha1
VERSION:=0.0.1-alpha3
# EXCLUDE_SRC_FROM_LIB= $(SRCDIR)/unpublishedfile.c
# this line excludes opcontrol.c and similar files
EXCLUDE_SRC_FROM_LIB+=$(foreach file, $(SRCDIR)/main,$(foreach cext,$(CEXTS),$(file).$(cext)) $(foreach cxxext,$(CXXEXTS),$(file).$(cxxext)))
Expand Down
82 changes: 55 additions & 27 deletions include/robotlog/robotlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ class LOGGER {
std::string COLOR_INFO = ROBOTLOG::Colors::GREEN;
std::string COLOR_DEBUG = ROBOTLOG::Colors::MAGENTA;
pros::Mutex logmutex;
std::ofstream file;

static void taskEntry(void *param) {
ROBOTLOG::LOGGER *logger = static_cast<ROBOTLOG::LOGGER *>(param);
Expand All @@ -260,7 +261,6 @@ class LOGGER {
pros::delay(10);
}

// std::ofstream file(this->filePath.value_or(""));
constexpr static char maxlogwrites = 10;
long availableLogs = this->logs.size();
short loopindices = 0;
Expand All @@ -274,8 +274,20 @@ class LOGGER {
LogMessage msg = this->logs.front();
std::string logmsg = msg.format(
this->logFormat.value_or("<CBLEVEL> <FILE>:<LINE> - <MESSAGE>"));
std::cout << logmsg << "\n";
// std::cout << logmsg << "\n";
logmsg = logmsg + "\n";
std::printf("%s", logmsg.c_str());
this->logs.pop();

if (file.is_open()) {
file << logmsg;
}
}
if (file.is_open()) {
file << std::flush;
file.flush();
file.close();
file.open(filePath.value(), std::ios::app);
}
pros::delay(5);
}
Expand All @@ -291,6 +303,19 @@ class LOGGER {
std::to_string(pros::millis()) + "ms");
}

/**
* @brief Construct a new LOGGER object
*
* @param filePath the file path to save logs to
*/
LOGGER(std::string filePath)
: worker(&taskEntry, this, "(VexLog) LogProcessor (File Enabled)") {
this->file.open(filePath, std::ios::app);
this->filePath = std::make_optional(filePath);
this->addlog(Level::debug, "Initalized VexLog @ " +
std::to_string(pros::millis()) + "ms");
}

/**
* @brief Add a log message to the queue
*
Expand Down Expand Up @@ -336,11 +361,11 @@ class LOGGER {

/** @brief Log a message with a specified log level
*
* Log a message with a specified log level. Automatically include the filename
* and line number in the log message.
* Log a message with a specified log level. Automatically include the
* filename and line number in the log message.
* @param level Log level
* @param message Log message
*/
*/
inline void ilog(ROBOTLOG::Level level, std::string message, std::string file,
int line) {
this->addlog(level, message, file, line);
Expand All @@ -351,7 +376,7 @@ class LOGGER {
* Log a message with log level INFO. Automatically include the filename
* and line number in the log message.
* @param message Log message
*/
*/
inline void info(std::string message) {
this->addlog(ROBOTLOG::Level::INFO, message, __FILENAME__, __LINE__);
}
Expand All @@ -361,7 +386,7 @@ class LOGGER {
* Log a message with log level DEBUG. Automatically include the filename
* and line number in the log message.
* @param message debug message
*/
*/
inline void debug(std::string message) {
this->addlog(ROBOTLOG::Level::DEBUG, message, __FILENAME__, __LINE__);
}
Expand All @@ -371,7 +396,7 @@ class LOGGER {
* Log a message with log level WARNING. Automatically include the filename
* and line number in the log message.
* @param message Log message
*/
*/
inline void warning(std::string message) {
this->addlog(ROBOTLOG::Level::WARNING, message, __FILENAME__, __LINE__);
}
Expand All @@ -381,7 +406,7 @@ class LOGGER {
* Log a message with log level ERROR. Automatically include the filename
* and line number in the log message.
* @param message Log message
*/
*/
inline void error(std::string message) {
this->addlog(ROBOTLOG::Level::ERROR, message, __FILENAME__, __LINE__);
}
Expand All @@ -391,24 +416,23 @@ class LOGGER {
* Log a message with log level DATA. Automatically include the filename
* and line number in the log message.
* @param message Log message
*/
*/
inline void data(std::string message) {
this->addlog(ROBOTLOG::Level::DATA, message, __FILENAME__, __LINE__);
}


/**
* @brief Set the Console Log Level
*
* Set the log level for the console. Only log messages with a level greater
* than or equal to the console log level will be printed to the console.
* @param level the new console log level
*/
void setConsoleLogLevel(ROBOTLOG::Level level) { this->consoleLogLevel = level; }

void setConsoleLogLevel(ROBOTLOG::Level level) {
this->consoleLogLevel = level;
}
};


// /**
// * Macro to generate log entries
// *
Expand All @@ -418,30 +442,33 @@ class LOGGER {
// * @param level Log level
// * @param message Log message
// */
#define log(level, message) LOGGER::ilog(level, message, __FILENAME__, __LINE__)
#define rlog(level, message) \
LOGGER::ilog(level, message, __FILENAME__, __LINE__)

// /**
// * @brief Macro to generate log entries with log level INFO
// *
// * Used when a info message is to be logged. Automatically include the filename
// * Used when a info message is to be logged. Automatically include the
// filename
// * and line number in the debug message.
// *
// * @param message Log message
// * @example info("This is an info message");
// */
#define info(message) LOGGER::info(message)
#define rinfo(message) LOGGER::info(message)

// /**
// * @brief Macro to generate log entries with log level DEBUG
// *
// * Used when an debug message is to be logged. Automatically include the filename
// * Used when an debug message is to be logged. Automatically include the
// filename
// * and line number in the info message.
// *
// * @param message debug message
// * @example debug("This is a debug message");
// */
// #define debug(message) \
// LOGGER::addlog(ROBOTLOG::Level::DEBUG, message, __FILENAME__, __LINE__)
#define rdebug(message) \
LOGGER::addlog(ROBOTLOG::Level::DEBUG, message, __FILENAME__, __LINE__)

// /**
// * @brief Macro to generate log entries with log level WARNING
Expand All @@ -452,8 +479,8 @@ class LOGGER {
// * @param message Log message
// * @example warning("This is a warning message");
// */
// #define warning(message) \
// LOGGER::addlog(ROBOTLOG::Level::WARNING, message, __FILENAME__, __LINE__)
#define rwarning(message) \
LOGGER::addlog(ROBOTLOG::Level::WARNING, message, __FILENAME__, __LINE__)

// /**
// * @brief Macro to generate log entries with log level ERROR
Expand All @@ -463,20 +490,21 @@ class LOGGER {
// * @param message Log message
// * @example error("This is an error message");
// */
// #define error(message) \
// LOGGER::addlog(ROBOTLOG::Level::ERROR, message, __FILENAME__, __LINE__)
#define rerror(message) \
LOGGER::addlog(ROBOTLOG::Level::ERROR, message, __FILENAME__, __LINE__)

// /**
// * @brief Macro to generate log entries with log level DATA. Only prints the
// * message, no formatting.
// *
// * Used when raw data should be printed/saved. Does not add any extra formatting
// * Used when raw data should be printed/saved. Does not add any extra
// formatting
// * to the message on the console or in the file.
// * @param message Log message
// * @example data("This is a data-only message");
// */
// #define data(message) \
// LOGGER::addlog(ROBOTLOG::Level::DATA, message, __FILENAME__, __LINE__)
#define rdata(message) \
LOGGER::addlog(ROBOTLOG::Level::DATA, message, __FILENAME__, __LINE__)

#endif

Expand Down
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

using namespace std;

ROBOTLOG::LOGGER logger;
ROBOTLOG::LOGGER logger("/usd/main.txt");

/**
* Runs initialization code. This occurs as soon as the program is started.
Expand Down Expand Up @@ -88,7 +88,7 @@ void opcontrol() {
int i = 0;
while (true) {
//logger.info("This is an info message"+ std::to_string(i));
logger.log(ROBOTLOG::Level(7), "This is a log message with a custom level of 7");
logger.rlog(ROBOTLOG::Level(i), "This is a log message with a custom level of i");
i++;
pros::delay(2);
}
Expand Down

0 comments on commit 8deecd6

Please sign in to comment.