source: trunk/src/sys/log.cpp @ 2257

Last change on this file since 2257 was 2257, checked in by sam, 10 years ago

core: move the Log and Timer classes to the sys/ subdirectory, use
the String class instead of the stack for logging, and get rid of
the old and useless BitField class.

  • Property svn:keywords set to Id
File size: 1.9 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://www.wtfpl.net/ for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <cstdio>
16
17#ifdef WIN32
18#   define WIN32_LEAN_AND_MEAN
19#   include <windows.h>
20#endif
21
22#if defined __ANDROID__
23#   include <android/log.h>
24#   include <unistd.h> /* for gettid() */
25#else
26#   include <cstdarg>
27#endif
28
29#include "core.h"
30
31namespace lol
32{
33
34/*
35 * Public Log class
36 */
37
38void Log::Debug(char const *fmt, ...)
39{
40    va_list ap;
41    va_start(ap, fmt);
42    Helper(DebugMessage, fmt, ap);
43    va_end(ap);
44}
45
46void Log::Info(char const *fmt, ...)
47{
48    va_list ap;
49    va_start(ap, fmt);
50    Helper(InfoMessage, fmt, ap);
51    va_end(ap);
52}
53
54void Log::Warn(char const *fmt, ...)
55{
56    va_list ap;
57    va_start(ap, fmt);
58    Helper(WarnMessage, fmt, ap);
59    va_end(ap);
60}
61
62void Log::Error(char const *fmt, ...)
63{
64    va_list ap;
65    va_start(ap, fmt);
66    Helper(ErrorMessage, fmt, ap);
67    va_end(ap);
68}
69
70/*
71 * Private helper function
72 */
73
74void Log::Helper(MessageType type, char const *fmt, va_list ap)
75{
76#if defined __ANDROID__
77    int const prio[] =
78    {
79        ANDROID_LOG_DEBUG,
80        ANDROID_LOG_INFO,
81        ANDROID_LOG_WARN,
82        ANDROID_LOG_ERROR
83    };
84
85    String buf = String::Printf(fmt, ap);
86    __android_log_print(prio[type], "LOL", "[%d] %s", (int)gettid(), &buf[0]);
87
88#else
89    char const *prefix[] =
90    {
91        "DEBUG",
92        "INFO",
93        "WARN",
94        "ERROR",
95    };
96
97#   if defined _WIN32
98    String buf = String(prefix[type]) + ": " + String::Printf(fmt, ap);
99    OutputDebugString(&buf[0]);
100#   else
101    fprintf(stderr, "%s: ", prefix[type]);
102    vfprintf(stderr, fmt, ap);
103#   endif
104#endif
105}
106
107} /* namespace lol */
108
Note: See TracBrowser for help on using the repository browser.