source: trunk/src/log.cpp @ 1943

Last change on this file since 1943 was 1943, checked in by sam, 7 years ago

android: print thread ID with all messages.

  • Property svn:keywords set to Id
File size: 2.0 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://sam.zoy.org/projects/COPYING.WTFPL 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    char buf[4096];
86    vsnprintf(buf, 4095, fmt, ap);
87    buf[4095] = '\0';
88
89    __android_log_print(prio[type], "LOL", "[%d] %s", (int)gettid(), buf);
90
91#else
92    char const *prefix[] =
93    {
94        "DEBUG",
95        "INFO",
96        "WARN",
97        "ERROR",
98    };
99
100#   if defined _WIN32
101    char buf[4096];
102    vsnprintf(buf, 4095, fmt, ap);
103    buf[4095] = '\0';
104    OutputDebugString(prefix[type]);
105    OutputDebugString(": ");
106    OutputDebugString(buf);
107#   else
108    fprintf(stderr, "%s: ", prefix[type]);
109    vfprintf(stderr, fmt, ap);
110#   endif
111#endif
112}
113
114} /* namespace lol */
115
Note: See TracBrowser for help on using the repository browser.