hx3d  1
2D/3D Simple Game Framework
log.cpp
1 /*
2  Log management.
3  Copyright (C) 2015 Denis BOURGE
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
21 #include "hx3d/utils/log.hpp"
22 
23 #include <iostream>
24 #include <sstream>
25 
26 #ifdef __ANDROID__
27  #include <android/log.h>
28  #define LOG(msg) __android_log_print(ANDROID_LOG_VERBOSE, "hx3d", msg, 1)
29 #elif __APPLE__
30  #include "TargetConditionals.h"
31  #ifdef TARGET_OS_IPHONE
32  #include <CoreFoundation/CoreFoundation.h>
33  extern "C" {
34  void NSLog(CFStringRef format, ...);
35  void NSLogv(CFStringRef format, va_list args);
36  }
37 
38  #define LOG(msg) NSLog(CFStringCreateWithCString(kCFAllocatorDefault, msg, kCFStringEncodingUTF8))
39  #else
40  #define LOG(msg) std::cout << msg << std::endl
41  #endif
42 #else
43  #define LOG(msg) std::cout << msg << std::endl
44 #endif
45 
46 namespace hx3d {
47 
48 LogImpl::LogImpl(): _consoleOutput(true) {
49 }
50 
51 LogImpl::~LogImpl() {
52 }
53 
54 void LogImpl::DisplayOnConsole(bool value) {
55  _consoleOutput = value;
56 }
57 
58 void LogImpl::Info(const std::string fmt, ...) {
59  va_list args;
60  va_start(args, fmt);
61  write(format(fmt, args), Status::Info);
62  va_end(args);
63 }
64 
65 void LogImpl::Shader(const std::string fmt, ...) {
66  va_list args;
67  va_start(args, fmt);
68  write(format(fmt, args), Status::Shader);
69  va_end(args);
70 }
71 
72 void LogImpl::Error(const std::string fmt, ...) {
73  va_list args;
74  va_start(args, fmt);
75  write(format(fmt, args), Status::Error);
76  va_end(args);
77 }
78 
80 
81 void LogImpl::write(std::string text, Status status) {
82 
83  std::ostringstream oss;
84  if (status == Status::Error) {
85  oss << "E /!\\> ";
86  }
87 
88  else if (status == Status::Shader) {
89  oss << "S> ";
90  }
91 
92  else if (status == Status::Info) {
93  oss << "I> ";
94  }
95 
96  oss << text;
97 
98  if (_consoleOutput) {
99  LOG(oss.str().c_str());
100  }
101 }
102 
103 }
std::string format(const std::string fmt,...)
Format a string using printf notation.
Definition: string.cpp:27
void DisplayOnConsole(bool value)
Display the log on the console.
Definition: log.cpp:54
hx3d framework namespace
Definition: audio.hpp:26
void Error(const std::string fmt,...)
Write an error message.
Definition: log.cpp:72
void Shader(const std::string fmt,...)
Write a shader message.
Definition: log.cpp:65
void Info(const std::string fmt,...)
Write an info message.
Definition: log.cpp:58