Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
15 KB
Referenced Files
None
Subscribers
None
diff --git a/util/debug.cpp b/util/debug.cpp
index 575ebef8..dc628eef 100644
--- a/util/debug.cpp
+++ b/util/debug.cpp
@@ -1,405 +1,431 @@
#include "debug.h"
+#include "system.h"
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#ifdef ANDROID
#include <android/log.h>
#define ANDROID_LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, "paintown", __VA_ARGS__)
#endif
#ifdef NETWORK_DEBUG
#include <sys/socket.h>
#include <sys/types.h>
#include <string.h>
#include <arpa/inet.h>
#endif
using namespace std;
static int global_debug_level = 0;
namespace Global{
#ifdef ANDROID
android_ostream::android_ostream(bool enabled):
enabled(enabled){
}
android_ostream & operator<<(android_ostream & stream, const std::string & input){
stream.buffer << input;
return stream;
}
android_ostream & operator<<(android_ostream & stream, const char * input){
stream.buffer << input;
return stream;
}
android_ostream & operator<<(android_ostream & stream, const char input){
stream.buffer << input;
return stream;
}
android_ostream & operator<<(android_ostream & stream, const double input){
stream.buffer << input;
return stream;
}
android_ostream & operator<<(android_ostream & stream, const int input){
stream.buffer << input;
return stream;
}
android_ostream & operator<<(android_ostream & stream, const short int input){
stream.buffer << input;
return stream;
}
android_ostream & operator<<(android_ostream & stream, const short unsigned int input){
stream.buffer << input;
return stream;
}
android_ostream & operator<<(android_ostream & stream, const unsigned int input){
stream.buffer << input;
return stream;
}
android_ostream & operator<<(android_ostream & stream, const bool input){
stream.buffer << input;
return stream;
}
android_ostream & operator<<(android_ostream & stream, const long int input){
stream.buffer << input;
return stream;
}
android_ostream & operator<<(android_ostream & stream, const unsigned long int input){
stream.buffer << input;
return stream;
}
android_ostream & operator<<(android_ostream & stream, const void * input){
stream.buffer << input;
return stream;
}
android_ostream & operator<<(android_ostream & stream, std::ostream & (*f)(std::ostream &)){
if (stream.enabled){
ANDROID_LOGV("%s\n", stream.buffer.str().c_str());
}
stream.buffer.str("");
stream.buffer.rdbuf()->pubseekoff(0, ios_base::end, ios_base::out);
stream.buffer.clear();
return stream;
}
android_ostream android_ostream::stream;
static android_ostream nullcout(false);
#elif defined(WII) && defined(DEBUG)
wii_ostream::wii_ostream(bool enabled):
enabled(enabled){
}
wii_ostream & operator<<(wii_ostream & stream, const std::string & input){
stream.buffer << input;
return stream;
}
wii_ostream & operator<<(wii_ostream & stream, const char * input){
stream.buffer << input;
return stream;
}
wii_ostream & operator<<(wii_ostream & stream, const char input){
stream.buffer << input;
return stream;
}
wii_ostream & operator<<(wii_ostream & stream, const double input){
stream.buffer << input;
return stream;
}
wii_ostream & operator<<(wii_ostream & stream, const int input){
stream.buffer << input;
return stream;
}
wii_ostream & operator<<(wii_ostream & stream, uint64_t input){
stream.buffer << input;
return stream;
}
wii_ostream & operator<<(wii_ostream & stream, const short int input){
stream.buffer << input;
return stream;
}
wii_ostream & operator<<(wii_ostream & stream, const short unsigned int input){
stream.buffer << input;
return stream;
}
wii_ostream & operator<<(wii_ostream & stream, const unsigned int input){
stream.buffer << input;
return stream;
}
wii_ostream & operator<<(wii_ostream & stream, const bool input){
stream.buffer << input;
return stream;
}
wii_ostream & operator<<(wii_ostream & stream, const long int input){
stream.buffer << input;
return stream;
}
wii_ostream & operator<<(wii_ostream & stream, const unsigned long int input){
stream.buffer << input;
return stream;
}
wii_ostream & operator<<(wii_ostream & stream, const void * input){
stream.buffer << input;
return stream;
}
wii_ostream & operator<<(wii_ostream & stream, std::ostream & (*f)(std::ostream &)){
if (stream.enabled){
printf("%s\n", stream.buffer.str().c_str());
}
stream.buffer.str("");
stream.buffer.rdbuf()->pubseekoff(0, ios_base::end, ios_base::out);
stream.buffer.clear();
return stream;
}
wii_ostream wii_ostream::stream;
static wii_ostream nullcout(false);
#elif defined(NETWORK_DEBUG)
network_ostream::network_ostream(const std::string & host, int port, bool enabled):
host(host),
port(port),
enabled(enabled){
}
network_ostream & operator<<(network_ostream & stream, const std::string & input){
stream.buffer << input;
return stream;
}
network_ostream & operator<<(network_ostream & stream, const char * input){
stream.buffer << input;
return stream;
}
network_ostream & operator<<(network_ostream & stream, const char input){
stream.buffer << input;
return stream;
}
network_ostream & operator<<(network_ostream & stream, const double input){
stream.buffer << input;
return stream;
}
network_ostream & operator<<(network_ostream & stream, const int input){
stream.buffer << input;
return stream;
}
network_ostream & operator<<(network_ostream & stream, uint64_t input){
stream.buffer << input;
return stream;
}
network_ostream & operator<<(network_ostream & stream, const short int input){
stream.buffer << input;
return stream;
}
network_ostream & operator<<(network_ostream & stream, const short unsigned int input){
stream.buffer << input;
return stream;
}
network_ostream & operator<<(network_ostream & stream, const unsigned int input){
stream.buffer << input;
return stream;
}
network_ostream & operator<<(network_ostream & stream, const bool input){
stream.buffer << input;
return stream;
}
network_ostream & operator<<(network_ostream & stream, const long int input){
stream.buffer << input;
return stream;
}
#ifndef PS3
network_ostream & operator<<(network_ostream & stream, const unsigned long int input){
stream.buffer << input;
return stream;
}
#endif
network_ostream & operator<<(network_ostream & stream, const void * input){
stream.buffer << input;
return stream;
}
static void sendString(const std::string & host, int port, const std::string & data){
int gateway = socket(PF_INET, SOCK_STREAM, 0);
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_port = htons(port);
address.sin_addr.s_addr = inet_addr(host.c_str());
memset(address.sin_zero, '\0', sizeof(address.sin_zero));
connect(gateway, (struct sockaddr*) &address, sizeof(address));
send(gateway, data.c_str(), data.size(), 0);
send(gateway, "\n", 1, 0);
close(gateway);
}
network_ostream & operator<<(network_ostream & stream, std::ostream & (*f)(std::ostream &)){
if (stream.enabled){
sendString(stream.host, stream.port, stream.buffer.str());
// printf("%s\n", stream.buffer.str().c_str());
}
stream.buffer.str("");
stream.buffer.rdbuf()->pubseekoff(0, ios_base::end, ios_base::out);
stream.buffer.clear();
return stream;
}
/* IP address and port are arbitrary */
network_ostream network_ostream::stream("192.168.1.100", 5670);
static network_ostream nullcout("", 0, false);
#else
class nullstreambuf_t: public std::streambuf {
public:
nullstreambuf_t():std::streambuf(){
}
};
static nullstreambuf_t nullstreambuf;
class nullcout_t: public std::ostream {
public:
nullcout_t():std::ostream(&nullstreambuf){
}
};
static nullcout_t nullcout;
#endif
#ifdef ANDROID
static stream_type & defaultStream(){
return android_ostream::stream;
}
static stream_type & getStream(){
return defaultStream();
}
void logToFile(){
}
void closeLog(){
}
#elif defined(WII) && defined(DEBUG)
static stream_type & defaultStream(){
return wii_ostream::stream;
}
static stream_type & getStream(){
return defaultStream();
}
void logToFile(){
}
void closeLog(){
}
#elif defined(NETWORK_DEBUG)
static stream_type & defaultStream(){
return network_ostream::stream;
}
static stream_type & getStream(){
return defaultStream();
}
void logToFile(){
}
void closeLog(){
}
#else
static stream_type & defaultStream(){
return std::cout;
}
static std::ofstream log;
static bool useFile = false;
static stream_type & fileStream(){
static bool init = false;
if (!init){
log.open("paintown.log");
init = true;
}
return log;
}
static void closeFileStream(){
log.close();
}
static stream_type & getStream(){
if (useFile){
return fileStream();
} else {
return defaultStream();
}
}
void logToFile(){
useFile = true;
}
void closeLog(){
closeFileStream();
}
#endif
}
Global::stream_type & Global::debug(int i, const string & context){
if (global_debug_level >= i){
Global::stream_type & out = getStream();
out << "[" << i << ":" << context << "] ";
return out;
}
return nullcout;
}
void Global::setDebug(int i){
global_debug_level = i;
}
int Global::getDebug(){
return global_debug_level;
}
std::string Global::defaultDebugContext = "default";
void Global::setDefaultDebugContext(const std::string & context){
defaultDebugContext = context;
}
+
+static std::string now(){
+ time_t t = time(NULL);
+ tm * timeData;
+ timeData = localtime(&t);
+ uint64_t millis = System::currentMilliseconds() % 1000;
+ if (timeData != NULL){
+ char buffer[200];
+ strftime(buffer, sizeof(buffer), "%F %I:%M:%S", timeData);
+ std::ostringstream out;
+ out << buffer << "." << millis;
+ strftime(buffer, sizeof(buffer), " %p", timeData);
+ out << buffer;
+ return out.str();
+ // return std::string(buffer);
+ }
+
+ return std::string();
+}
+
+std::string Global::debug_context(const char * file, int line){
+ std::ostringstream out;
+ out << file << ":" << line << " " << now();
+ return out.str();
+}
diff --git a/util/debug.h b/util/debug.h
index 86e13dee..14ebe527 100644
--- a/util/debug.h
+++ b/util/debug.h
@@ -1,110 +1,113 @@
#ifndef _paintown_debug_h
#define _paintown_debug_h
#include <ostream>
#include <sstream>
#include <stdint.h>
+#define PAINTOWN_DEBUG_CONTEXT Global::debug_context(__FILE__, __LINE__)
+
/* Enable this if you can't get regular debug output but have networking
*/
// #define NETWORK_DEBUG
namespace Global{
#ifdef ANDROID
class android_ostream: public std::ostream {
public:
android_ostream(bool enabled = true);
static android_ostream stream;
/* make these private at some point */
public:
bool enabled;
std::ostringstream buffer;
};
typedef android_ostream stream_type;
android_ostream & operator<<(android_ostream & stream, const std::string & input);
android_ostream & operator<<(android_ostream & stream, const char * input);
android_ostream & operator<<(android_ostream & stream, const char);
android_ostream & operator<<(android_ostream & stream, const double);
android_ostream & operator<<(android_ostream & stream, const int);
android_ostream & operator<<(android_ostream & stream, const short int);
android_ostream & operator<<(android_ostream & stream, const short unsigned int);
android_ostream & operator<<(android_ostream & stream, const unsigned int);
android_ostream & operator<<(android_ostream & stream, const bool);
android_ostream & operator<<(android_ostream & stream, const long int);
android_ostream & operator<<(android_ostream & stream, const unsigned long int);
android_ostream & operator<<(android_ostream & stream, const void *);
android_ostream & operator<<(android_ostream & stream, std::ostream & (*f)(std::ostream &));
#elif defined(WII) && defined(DEBUG)
class wii_ostream: public std::ostream {
public:
wii_ostream(bool enabled = true);
static wii_ostream stream;
/* make these private at some point */
public:
bool enabled;
std::ostringstream buffer;
};
typedef wii_ostream stream_type;
wii_ostream & operator<<(wii_ostream & stream, const std::string & input);
wii_ostream & operator<<(wii_ostream & stream, const char * input);
wii_ostream & operator<<(wii_ostream & stream, const char);
wii_ostream & operator<<(wii_ostream & stream, const double);
wii_ostream & operator<<(wii_ostream & stream, const int);
wii_ostream & operator<<(wii_ostream & stream, const short int);
wii_ostream & operator<<(wii_ostream & stream, const short unsigned int);
wii_ostream & operator<<(wii_ostream & stream, const unsigned int);
wii_ostream & operator<<(wii_ostream & stream, const bool);
wii_ostream & operator<<(wii_ostream & stream, const long int);
wii_ostream & operator<<(wii_ostream & stream, const unsigned long int);
wii_ostream & operator<<(wii_ostream & stream, const void *);
wii_ostream & operator<<(wii_ostream & stream, uint64_t);
wii_ostream & operator<<(wii_ostream & stream, std::ostream & (*f)(std::ostream &));
#elif defined(NETWORK_DEBUG)
class network_ostream: public std::ostream {
public:
network_ostream(const std::string & host, int port, bool enabled = true);
static network_ostream stream;
/* make these private at some point */
public:
std::string host;
int port;
bool enabled;
std::ostringstream buffer;
};
typedef network_ostream stream_type;
stream_type & operator<<(stream_type & stream, const std::string & input);
stream_type & operator<<(stream_type & stream, const char * input);
stream_type & operator<<(stream_type & stream, const char);
stream_type & operator<<(stream_type & stream, const double);
stream_type & operator<<(stream_type & stream, const int);
stream_type & operator<<(stream_type & stream, const short int);
stream_type & operator<<(stream_type & stream, const short unsigned int);
stream_type & operator<<(stream_type & stream, const unsigned int);
stream_type & operator<<(stream_type & stream, const bool);
stream_type & operator<<(stream_type & stream, const long int);
#ifndef PS3
stream_type & operator<<(stream_type & stream, const unsigned long int);
#endif
stream_type & operator<<(stream_type & stream, const void *);
stream_type & operator<<(stream_type & stream, uint64_t);
stream_type & operator<<(stream_type & stream, std::ostream & (*f)(std::ostream &));
#else
typedef std::ostream stream_type;
#endif
void logToFile();
void closeLog();
void setDebug(int i);
int getDebug();
extern std::string defaultDebugContext;
void setDefaultDebugContext(const std::string & context);
stream_type & debug(int i, const std::string & context = defaultDebugContext);
+std::string debug_context(const char * file, int line);
}
#endif

File Metadata

Mime Type
text/x-diff
Expires
Wed, Jun 17, 9:18 PM (1 w, 5 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
70049
Default Alt Text
(15 KB)

Event Timeline