log/sink: simply and rename the ostream sink as path

This commit is contained in:
Danny Robson 2020-04-21 12:42:05 +10:00
parent 1f25f780ee
commit 607031a041
5 changed files with 31 additions and 28 deletions

View File

@ -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

View File

@ -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.

View File

@ -13,7 +13,7 @@
#include "../string.hpp"
#include "sink/console.hpp"
#include "sink/ostream.hpp"
#include "sink/path.hpp"
#include <iostream>
#include <mutex>
@ -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<sink::ostream> (
PACKAGE_NAME,
log_path
)
);
} else if (strcmp (log_sink, "CONSOLE")) {
default_sink (
std::make_unique<sink::console> (PACKAGE_NAME)
);
if (!strcmp (log_sink, "PATH")) {
default_sink (std::make_unique<sink::path> (PACKAGE_NAME));
} else if (!strcmp (log_sink, "CONSOLE")) {
default_sink (std::make_unique<sink::console> (PACKAGE_NAME));
} else {
std::clog << "Unknown logging sink. Falling back to console.\n";
default_sink (
std::make_unique<sink::console> (PACKAGE_NAME)
);
default_sink (std::make_unique<sink::console> (PACKAGE_NAME));
}
s_sink_init = true;

View File

@ -6,24 +6,37 @@
* Copyright 2020, Danny Robson <danny@nerdcruft.net>
*/
#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;

View File

@ -16,12 +16,9 @@
namespace cruft::log::sink {
class ostream : public crtp<ostream> {
class path : public crtp<path> {
public:
ostream (
std::string name,
std::filesystem::path const &path
);
path (std::string name);
void write (packet const&) override;