3 Commits

Author SHA1 Message Date
e5d8ea5faa Removed unused include "format" 2024-06-28 09:43:31 -04:00
e870007004 Beginning documentation effort. 2024-06-27 15:09:10 -04:00
4138b45404 Implement basic capability to log to specified file. 2024-06-27 14:57:14 -04:00
3 changed files with 59 additions and 12 deletions

View File

@@ -3,10 +3,8 @@
/// Developed & Maintained by Josh O'Leary
/// This work is dedicated to the public domain.
#pragma once
#include <format>
#include <string>
#include <Event.h>
#include <jlog/color_codes.hpp>
@@ -21,14 +19,16 @@
namespace jlog
{
/// Severity levels for logging. Higher numbers filter less messages out.
/// @see LOG_LEVEL macro
enum class severity : uint8_t
{
none,
warning,
error,
fatal,
verbose,
debug,
none, // Show no output
fatal, // Show only fatal errors
error, // Show fatal and not-necessarily-fatal errors
warning, // Show warnings additionally
verbose, // Show errors, warnings, and 'verbose' output (i.e. extra information)
debug, // Show debugging messages (Basically everything).
};
inline severity loglevel = severity::debug; // Default log level always debug
@@ -40,10 +40,13 @@ namespace jlog
std::string timestamp;
};
// TODO: Fully implement logging callback.
static Event<LogEntry> on_log = Event<LogEntry>();
inline std::vector<LogEntry> log_history;
/// A single piece of a logging message, with color code, content, and delimiter
/// These are strung together to build full logger messages in a flexible manner.
struct token
{
colorcode colorCode = color_codes::FG_DEFAULT;
@@ -51,13 +54,34 @@ namespace jlog
std::string delimiter = "[]";
};
std::string get_timestamp();
void log_to_console(const std::string& message);
void log_to_file(const std::string& message);
void set_default_logfile(const std::string& filename);
std::string get_timestamp();
/// Writes an input string directly to standard output
/// @note Does not append a newline to the message.
void log_to_console(const std::string& message);
/// Writes an input string directly to the default destination logfile
/// @note Does not append a newline to the message.
/// @see set_default_logfile()
void log_to_file(const std::string& message);
/// Writes an input string directly to the specified destination logfile
/// @note Does not append a newline to the message.
void log_to_file(const std::string& filename, const std::string& message);
/// Writes an input string directly to the specified output stream
/// @note Does not append a newline to the message.
void log_to_stream(std::ostream stream, const std::string& message);
/// Parses a sequence of tokens into a printable message, and logs to the following destinations:
/// standard output (console)
/// output file (default)
/// logging callback (Event)
/// @note This file is implemented differently per-platform to handle differences in console color handling.
/// @see windows/jlog.cpp linux/jlog.cpp
void log(std::vector<token> tokens);
// Generic formatters for building loggers.
std::vector<token> trace_format(
const std::string& func,
const std::string& file,

View File

@@ -5,6 +5,15 @@
namespace jlog
{
static std::string default_logfile = "latest.log";
void set_default_logfile(const std::string& filename)
{
default_logfile = filename;
}
std::string get_timestamp()
{
using namespace std::chrono;
@@ -29,11 +38,23 @@ namespace jlog
void log_to_file(const std::string& message)
{
std::ofstream latest_log("latest.log", std::ios_base::app);
std::ofstream latest_log(default_logfile, std::ios_base::app);
latest_log << message;
latest_log.close();
}
void log_to_file(const std::string& filename, const std::string& message)
{
std::ofstream latest_log(filename, std::ios_base::app);
latest_log << message;
latest_log.close();
}
void log_to_stream(std::ostream stream, const std::string& message)
{
stream << message;
}
std::vector<token> trace_format(
const std::string& func,
const std::string& file,

View File

@@ -1,3 +1,5 @@
#include <jlog/jlog.hpp>
#include <string>