Browse Source

Implement <lol/msg> to replace <lol/base/log.h> in the future.

pull/1/head
Sam Hocevar 4 years ago
parent
commit
7e540d8eff
2 changed files with 110 additions and 0 deletions
  1. +18
    -0
      include/lol/msg
  2. +92
    -0
      include/lol/private/base/msg.h

+ 18
- 0
include/lol/msg View File

@@ -0,0 +1,18 @@
//
// Lol Engine
//
// Copyright © 2010—2020 Sam Hocevar <sam@hocevar.net>
//
// Lol Engine is free software. It comes without any warranty, to
// the extent permitted by applicable law. You can redistribute it
// and/or modify it under the terms of the Do What the Fuck You Want
// to Public License, Version 2, as published by the WTFPL Task Force.
// See http://www.wtfpl.net/ for more details.
//

#pragma once

#include "private/push_macros.h"
#include "private/base/msg.h"
#include "private/pop_macros.h"


+ 92
- 0
include/lol/private/base/msg.h View File

@@ -0,0 +1,92 @@
//
// Lol Engine
//
// Copyright © 2010—2020 Sam Hocevar <sam@hocevar.net>
//
// Lol Engine is free software. It comes without any warranty, to
// the extent permitted by applicable law. You can redistribute it
// and/or modify it under the terms of the Do What the Fuck You Want
// to Public License, Version 2, as published by the WTFPL Task Force.
// See http://www.wtfpl.net/ for more details.
//

#pragma once

#if !_DEBUG
#include "../base/env.h" // sys::getenv
#endif

#include <cstdio> // std::vfprintf

namespace lol
{

struct msg
{
template<typename... T>
static inline void debug(char const *fmt, T... args)
{
helper(message_type::debug, fmt, args...);
}

template<typename... T>
static inline void info(char const *fmt, T... args)
{
helper(message_type::info, fmt, args...);
}

template<typename... T>
static inline void warn(char const *fmt, T... args)
{
helper(message_type::warn, fmt, args...);
}

template<typename... T>
static inline void error(char const *fmt, T... args)
{
helper(message_type::error, fmt, args...);
}

private:
msg() = delete;

enum class message_type
{
debug,
info,
warn,
error,
};

static void helper(message_type type, char const *fmt, ...)
{
// Unless this is a debug build, ignore debug messages unless
// the LOL_DEBUG environment variable is set.
#if !_DEBUG
if (type == message_type::debug)
{
static bool const disable_debug = sys::getenv("LOL_DEBUG").size() > 0;
if (disable_debug)
return;
}
#endif

static char const * const prefix[] =
{
"DEBUG",
"INFO",
"WARN",
"ERROR",
};

fprintf(stdout, "%s: ", prefix[int(type)]);
va_list ap;
va_start(ap, fmt);
vfprintf(stdout, fmt, ap);
va_end(ap);
fflush(stdout);
}
};

} // namespace lol


Loading…
Cancel
Save