Add she::Logger to log errors in Mac OS X console

This commit is contained in:
David Capello 2014-08-24 00:11:51 -03:00
parent be0b7de49e
commit 4921eeca9c
5 changed files with 71 additions and 2 deletions

View File

@ -5,8 +5,12 @@ set(SHE_SOURCES
alleg4/fontbmp.cpp alleg4/fontbmp.cpp
alleg4/she_alleg4.cpp) alleg4/she_alleg4.cpp)
if(APPLE AND NOT USE_SHARED_ALLEGRO4) if(APPLE)
if(NOT USE_SHARED_ALLEGRO4)
list(APPEND SHE_SOURCES alleg4/app.mm) list(APPEND SHE_SOURCES alleg4/app.mm)
endif() endif()
list(APPEND SHE_SOURCES osx/logger.mm)
endif()
add_library(she ${SHE_SOURCES}) add_library(she ${SHE_SOURCES})

View File

@ -14,6 +14,7 @@
#include "base/string.h" #include "base/string.h"
#include "she/alleg4/alleg4_font.h" #include "she/alleg4/alleg4_font.h"
#include "she/alleg4/alleg4_surface.h" #include "she/alleg4/alleg4_surface.h"
#include "she/logger.h"
#include <allegro.h> #include <allegro.h>
#include <allegro/internal/aintern.h> #include <allegro/internal/aintern.h>
@ -22,6 +23,7 @@
#include <winalleg.h> #include <winalleg.h>
#include <windowsx.h> #include <windowsx.h>
#include <commctrl.h> #include <commctrl.h>
#if defined STRICT || defined __GNUC__ #if defined STRICT || defined __GNUC__
@ -78,6 +80,10 @@ static void resize_callback(RESIZE_DISPLAY_EVENT* ev)
namespace she { namespace she {
#ifdef __APPLE__
Logger* getOsxLogger();
#endif
class Alleg4EventQueue : public EventQueue { class Alleg4EventQueue : public EventQueue {
public: public:
Alleg4EventQueue() { Alleg4EventQueue() {
@ -681,6 +687,14 @@ public:
); );
} }
Logger* logger() override {
#ifdef __APPLE__
return getOsxLogger();
#else
return NULL;
#endif
}
Display* defaultDisplay() override { Display* defaultDisplay() override {
return unique_display; return unique_display;
} }
@ -740,6 +754,9 @@ System* instance()
void error_message(const char* msg) void error_message(const char* msg)
{ {
if (g_instance && g_instance->logger())
g_instance->logger()->logError(msg);
#ifdef WIN32 #ifdef WIN32
std::wstring wmsg = base::from_utf8(msg); std::wstring wmsg = base::from_utf8(msg);
std::wstring title = base::from_utf8(PACKAGE); std::wstring title = base::from_utf8(PACKAGE);

21
src/she/logger.h Normal file
View File

@ -0,0 +1,21 @@
// SHE library
// Copyright (C) 2012-2014 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifndef SHE_LOGGER_H_INCLUDED
#define SHE_LOGGER_H_INCLUDED
#pragma once
namespace she {
class Logger {
public:
virtual ~Logger() { }
virtual void logError(const char* error) = 0;
};
} // namespace she
#endif

25
src/she/osx/logger.mm Normal file
View File

@ -0,0 +1,25 @@
// SHE library
// Copyright (C) 2012-2014 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#include <CoreFoundation/CoreFoundation.h>
#include <Foundation/Foundation.h>
#include "she/logger.h"
namespace she {
class OSXLogger : public Logger {
public:
void logError(const char* error) override {
NSLog([NSString stringWithUTF8String:error]);
}
};
Logger* getOsxLogger() {
return new OSXLogger;
}
} // namespace she

View File

@ -17,6 +17,7 @@ namespace she {
class Display; class Display;
class EventLoop; class EventLoop;
class Font; class Font;
class Logger;
class Surface; class Surface;
class DisplayCreationException : std::runtime_error { class DisplayCreationException : std::runtime_error {
@ -30,6 +31,7 @@ namespace she {
virtual ~System() { } virtual ~System() { }
virtual void dispose() = 0; virtual void dispose() = 0;
virtual Capabilities capabilities() const = 0; virtual Capabilities capabilities() const = 0;
virtual Logger* logger() = 0;
virtual Display* defaultDisplay() = 0; virtual Display* defaultDisplay() = 0;
virtual Font* defaultFont() = 0; virtual Font* defaultFont() = 0;
virtual Display* createDisplay(int width, int height, int scale) = 0; virtual Display* createDisplay(int width, int height, int scale) = 0;