source: trunk/src/log.cpp @ 1695

Last change on this file since 1695 was 1695, checked in by sam, 8 years ago

android: simplify the logger code.

  • 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#else
25#   include <cstdarg>
26#endif
27
28#include "core.h"
29
30namespace lol
31{
32
33/*
34 * Public Log class
35 */
36
37void Log::Debug(char const *fmt, ...)
38{
39    va_list ap;
40    va_start(ap, fmt);
41    Helper(DebugMessage, fmt, ap);
42    va_end(ap);
43}
44
45void Log::Info(char const *fmt, ...)
46{
47    va_list ap;
48    va_start(ap, fmt);
49    Helper(InfoMessage, fmt, ap);
50    va_end(ap);
51}
52
53void Log::Warn(char const *fmt, ...)
54{
55    va_list ap;
56    va_start(ap, fmt);
57    Helper(WarnMessage, fmt, ap);
58    va_end(ap);
59}
60
61void Log::Error(char const *fmt, ...)
62{
63    va_list ap;
64    va_start(ap, fmt);
65    Helper(ErrorMessage, fmt, ap);
66    va_end(ap);
67}
68
69/*
70 * Private helper function
71 */
72
73void Log::Helper(MessageType type, char const *fmt, va_list ap)
74{
75#if defined __ANDROID__
76    int prio[] =
77    {
78        ANDROID_LOG_DEBUG,
79        ANDROID_LOG_INFO,
80        ANDROID_LOG_WARN,
81        ANDROID_LOG_ERROR
82    };
83
84    //__android_log_print(prio[type], "LOL", "thread %ld", pthread_self());
85    __android_log_vprint(prio[type], "LOL", fmt, ap);
86
87#else
88    char const *prefix[] =
89    {
90        "DEBUG",
91        "INFO",
92        "WARN",
93        "ERROR",
94    };
95
96#   if defined _WIN32
97    char buf[4096];
98    vsnprintf(buf, 4095, fmt, ap);
99    buf[4095] = '\0';
100    OutputDebugString(prefix[type]);
101    OutputDebugString(": ");
102    OutputDebugString(buf);
103#   else
104    fprintf(stderr, "%s: ", prefix[type]);
105    vfprintf(stderr, fmt, ap);
106#   endif
107#endif
108}
109
110} /* namespace lol */
111
Note: See TracBrowser for help on using the repository browser.