diff --git a/CMakeLists.txt b/CMakeLists.txt index 5869c628..c48a8365 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -448,8 +448,8 @@ list ( log/sink/console.hpp log/sink/null.cpp log/sink/null.hpp - log/sink/ostream.cpp - log/sink/ostream.hpp + log/sink/path.cpp + log/sink/path.hpp map/fixed.cpp map/fixed.hpp maths.cpp diff --git a/README.adoc b/README.adoc index adeced2a..a2084d38 100644 --- a/README.adoc +++ b/README.adoc @@ -14,6 +14,9 @@ DEBUG_WAIT:: whether to wait for a debugger to attach before executing `main`. LOG_LEVEL:: minimum log level that will render to the logging stream. note that a given level may have been compiled out and may not be present, eg. DEBUG tends to only be present for debug builds. BREAK_LEVEL:: minimum log level that will trigger a breakpoint +CRUFT_LOG_SINK:: The output driver for logging. One of 'CONSOLE', 'PATH'. +CRUFT_LOG_PATH:: The output path for the 'PATH' logging driver. This path is subject to path expansion and will be interpreted as relative to the current working directory if it is relative. + JOB_THREADS:: default number of threads to spin up for job queues. JOB_DEPTH:: the default size of the pending work item queue for a job queue. diff --git a/log/log.cpp b/log/log.cpp index 925d6799..70e90bc3 100644 --- a/log/log.cpp +++ b/log/log.cpp @@ -13,7 +13,7 @@ #include "../string.hpp" #include "sink/console.hpp" -#include "sink/ostream.hpp" +#include "sink/path.hpp" #include #include @@ -68,23 +68,13 @@ cruft::log::default_sink () static char const *DEFAULT_SINK = "CONSOLE"; char const *log_sink = getenv ("CRUFT_LOG_SINK") ?: DEFAULT_SINK; - if (!strcmp (log_sink, "FILE")) { - char const *log_path = getenv ("CRUFT_LOG_PATH"); - default_sink ( - std::make_unique ( - PACKAGE_NAME, - log_path - ) - ); - } else if (strcmp (log_sink, "CONSOLE")) { - default_sink ( - std::make_unique (PACKAGE_NAME) - ); + if (!strcmp (log_sink, "PATH")) { + default_sink (std::make_unique (PACKAGE_NAME)); + } else if (!strcmp (log_sink, "CONSOLE")) { + default_sink (std::make_unique (PACKAGE_NAME)); } else { std::clog << "Unknown logging sink. Falling back to console.\n"; - default_sink ( - std::make_unique (PACKAGE_NAME) - ); + default_sink (std::make_unique (PACKAGE_NAME)); } s_sink_init = true; diff --git a/log/sink/ostream.cpp b/log/sink/path.cpp similarity index 69% rename from log/sink/ostream.cpp rename to log/sink/path.cpp index 018cd1d8..6ce8c736 100644 --- a/log/sink/ostream.cpp +++ b/log/sink/path.cpp @@ -6,24 +6,37 @@ * Copyright 2020, Danny Robson */ -#include "ostream.hpp" +#include "path.hpp" #include "../level.hpp" #include "../packet.hpp" +#include "../../paths.hpp" -using cruft::log::sink::ostream; +using cruft::log::sink::path; /////////////////////////////////////////////////////////////////////////////// -ostream::ostream (std::string name, std::filesystem::path const &dst) +static std::filesystem::path +find_path (void) +{ + if (char const *log_path = getenv ("CRUFT_LOG_PATH"); log_path) + return cruft::paths::expand (log_path); + + static constexpr char DEFAULT_PATH[] = PACKAGE_NAME ".log"; + return std::filesystem::current_path () / DEFAULT_PATH; +} + + +/////////////////////////////////////////////////////////////////////////////// +path::path (std::string name) : crtp (std::move (name)) - , m_output (dst) + , m_output (find_path ()) { ; } /////////////////////////////////////////////////////////////////////////////// void -ostream::write (packet const &val) +path::write (packet const &val) { if (val.level > log_level ()) return; diff --git a/log/sink/ostream.hpp b/log/sink/path.hpp similarity index 77% rename from log/sink/ostream.hpp rename to log/sink/path.hpp index 564b46d4..41e8b42b 100644 --- a/log/sink/ostream.hpp +++ b/log/sink/path.hpp @@ -16,12 +16,9 @@ namespace cruft::log::sink { - class ostream : public crtp { + class path : public crtp { public: - ostream ( - std::string name, - std::filesystem::path const &path - ); + path (std::string name); void write (packet const&) override;