diff --git a/interface/src/Log.cpp b/interface/src/Log.cpp index 5f5381addd..3bfbe19a93 100644 --- a/interface/src/Log.cpp +++ b/interface/src/Log.cpp @@ -144,10 +144,7 @@ inline void Log::addMessage(char const* ptr) { } -void Log::operator()(char const* fmt, ...) { - - va_list args; - va_start(args,fmt); +int Log::vprint(char const* fmt, va_list args) { pthread_mutex_lock(& _mtx); // print to buffer @@ -166,6 +163,14 @@ void Log::operator()(char const* fmt, ...) { } pthread_mutex_unlock(& _mtx); + return n; +} + +void Log::operator()(char const* fmt, ...) { + + va_list args; + va_start(args,fmt); + vprint(fmt, args); va_end(args); } @@ -304,8 +309,13 @@ void Log::render(unsigned screenWidth, unsigned screenHeight) { glMatrixMode(matrixMode); } -Log printLog; - +Log logger; +int printLog(char const* fmt, ...) { + va_list args; + va_start(args,fmt); + logger.vprint(fmt, args); + va_end(args); +} diff --git a/interface/src/Log.h b/interface/src/Log.h index bb74f33933..7def966c32 100644 --- a/interface/src/Log.h +++ b/interface/src/Log.h @@ -10,6 +10,7 @@ #define __interface__Log__ #include +#include #include #include @@ -18,7 +19,13 @@ class Log; // // Call it as you would call 'printf'. // -extern Log printLog; +int printLog(char const* fmt, ...); + +// +// Global instance. +// +extern Log logger; + // // Logging subsystem. @@ -57,6 +64,7 @@ public: void render(unsigned screenWidth, unsigned screenHeight); void operator()(char const* fmt, ...); + int vprint(char const* fmt, va_list); private: // don't copy/assign diff --git a/interface/src/main.cpp b/interface/src/main.cpp index 3248dae02c..d40129580b 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -889,7 +889,7 @@ void display(void) glLineWidth(1.0f); glPointSize(1.0f); displayStats(); - printLog.render(WIDTH, HEIGHT); + logger.render(WIDTH, HEIGHT); } // Show menu @@ -1514,6 +1514,9 @@ void audioMixerUpdate(in_addr_t newMixerAddress, in_port_t newMixerPort) { int main(int argc, const char * argv[]) { + shared::printLog = ::printLog; + + // Quick test of the Orientation class on startup! testOrientationClass(); diff --git a/libraries/shared/src/AgentList.cpp b/libraries/shared/src/AgentList.cpp index 2751527d37..05e6c5db06 100644 --- a/libraries/shared/src/AgentList.cpp +++ b/libraries/shared/src/AgentList.cpp @@ -21,6 +21,8 @@ #include #endif +using shared::printLog; + const char * SOLO_AGENT_TYPES_STRING = "MV"; char DOMAIN_HOSTNAME[] = "highfidelity.below92.com"; char DOMAIN_IP[100] = ""; // IP Address will be re-set by lookup on startup @@ -37,7 +39,7 @@ AgentList* AgentList::createInstance(char ownerType, unsigned int socketListenPo if (_sharedInstance == NULL) { _sharedInstance = new AgentList(ownerType, socketListenPort); } else { - printf("AgentList createInstance called with existing instance.\n"); + printLog("AgentList createInstance called with existing instance.\n"); } return _sharedInstance; @@ -45,7 +47,7 @@ AgentList* AgentList::createInstance(char ownerType, unsigned int socketListenPo AgentList* AgentList::getInstance() { if (_sharedInstance == NULL) { - printf("AgentList getInstance called before call to createInstance. Returning NULL pointer.\n"); + printLog("AgentList getInstance called before call to createInstance. Returning NULL pointer.\n"); } return _sharedInstance; @@ -391,12 +393,12 @@ void *checkInWithDomainServer(void *args) { sockaddr_in tempAddress; memcpy(&tempAddress.sin_addr, pHostInfo->h_addr_list[0], pHostInfo->h_length); strcpy(DOMAIN_IP, inet_ntoa(tempAddress.sin_addr)); - printf("Domain server: %s \n", DOMAIN_HOSTNAME); + printLog("Domain server: %s \n", DOMAIN_HOSTNAME); } else { - printf("Failed lookup domainserver\n"); + printLog("Failed lookup domainserver\n"); } - } else printf("Using static domainserver IP: %s\n", DOMAIN_IP); + } else printLog("Using static domainserver IP: %s\n", DOMAIN_IP); while (!domainServerCheckinStopFlag) { diff --git a/libraries/shared/src/AgentList.h b/libraries/shared/src/AgentList.h index e01147db9f..e8ff98fe80 100644 --- a/libraries/shared/src/AgentList.h +++ b/libraries/shared/src/AgentList.h @@ -12,6 +12,8 @@ #include #include #include + +#include "shared_Log.h" #include "Agent.h" #include "UDPSocket.h" diff --git a/libraries/shared/src/CounterStats.cpp b/libraries/shared/src/CounterStats.cpp index b86cfd37f2..51d991f0f1 100644 --- a/libraries/shared/src/CounterStats.cpp +++ b/libraries/shared/src/CounterStats.cpp @@ -19,7 +19,7 @@ #endif #include #include - +#include "shared_Log.h" //private: // long int currentCount; @@ -98,7 +98,7 @@ void CounterStatHistory::recordSample(double thisTime, long thisCount) { this->currentTime = thisTime; this->currentDelta = thisDelta; - //printf("CounterStatHistory[%s]::recordSample(thisTime %lf, thisCount= %ld)\n",this->name.c_str(),thisTime,thisCount); + //printLog("CounterStatHistory[%s]::recordSample(thisTime %lf, thisCount= %ld)\n",this->name.c_str(),thisTime,thisCount); // if more than 1/10th of a second has passed, then record // things in our rolling history @@ -115,7 +115,7 @@ void CounterStatHistory::recordSample(double thisTime, long thisCount) { this->timeSamples[this->sampleAt]=thisTime; this->deltaSamples[this->sampleAt]=thisDelta; - //printf("CounterStatHistory[%s]::recordSample() ACTUALLY RECORDING IT sampleAt=%d thisTime %lf, thisCount= %ld)\n",this->name.c_str(),this->sampleAt,thisTime,thisCount); + //printLog("CounterStatHistory[%s]::recordSample() ACTUALLY RECORDING IT sampleAt=%d thisTime %lf, thisCount= %ld)\n",this->name.c_str(),this->sampleAt,thisTime,thisCount); } diff --git a/libraries/shared/src/PerfStat.cpp b/libraries/shared/src/PerfStat.cpp index a6e849aeb0..a312f65c1b 100644 --- a/libraries/shared/src/PerfStat.cpp +++ b/libraries/shared/src/PerfStat.cpp @@ -14,6 +14,10 @@ #include #include +#include "shared_Log.h" + +using shared::printLog; + // Static class members initialization here! std::map > PerfStat::groupHistoryMap; bool PerfStat::wantDebugOut = false; @@ -55,7 +59,7 @@ PerfStat::~PerfStat() { } if (wantDebugOut) { - printf("PerfStats: %s elapsed:%f average:%lf count:%ld total:%lf ut:%d us:%d ue:%d t:%ld s:%ld e:%ld\n", + printLog("PerfStats: %s elapsed:%f average:%lf count:%ld total:%lf ut:%d us:%d ue:%d t:%ld s:%ld e:%ld\n", this->group.c_str(),elapsed,average,count,totalTime, (end.tv_usec-start.tv_usec),start.tv_usec,end.tv_usec, (end.tv_sec-start.tv_sec),start.tv_sec,end.tv_sec diff --git a/libraries/shared/src/SharedUtil.cpp b/libraries/shared/src/SharedUtil.cpp index 8fb8645730..dbd94a089d 100644 --- a/libraries/shared/src/SharedUtil.cpp +++ b/libraries/shared/src/SharedUtil.cpp @@ -12,6 +12,7 @@ #ifdef _WIN32 #include "Syssocket.h" #endif +#include "shared_Log.h" #include "SharedUtil.h" #include "OctalCode.h" @@ -19,6 +20,8 @@ #include #endif +using shared::printLog; + double usecTimestamp(timeval *time) { return (time->tv_sec * 1000000.0 + time->tv_usec); } @@ -50,13 +53,13 @@ bool randomBoolean() { } void outputBits(unsigned char byte) { - printf("%d: ", byte); + printLog("%d: ", byte); for (int i = 0; i < 8; i++) { - printf("%d", byte >> (7 - i) & 1); + printLog("%d", byte >> (7 - i) & 1); } - printf("\n"); + printLog("\n"); } int numberOfOnes(unsigned char byte) { @@ -328,14 +331,14 @@ void printVoxelCode(unsigned char* voxelCode) { unsigned int voxelSizeInOctets = (voxelSizeInBits/3); unsigned int voxelBufferSize = voxelSizeInBytes+1+3; // 1 for size, 3 for color - printf("octets=%d\n",octets); - printf("voxelSizeInBits=%d\n",voxelSizeInBits); - printf("voxelSizeInBytes=%d\n",voxelSizeInBytes); - printf("voxelSizeInOctets=%d\n",voxelSizeInOctets); - printf("voxelBufferSize=%d\n",voxelBufferSize); + printLog("octets=%d\n",octets); + printLog("voxelSizeInBits=%d\n",voxelSizeInBits); + printLog("voxelSizeInBytes=%d\n",voxelSizeInBytes); + printLog("voxelSizeInOctets=%d\n",voxelSizeInOctets); + printLog("voxelBufferSize=%d\n",voxelBufferSize); for(int i=0;i #endif +#include "shared_Log.h" + +using shared::printLog; + sockaddr_in destSockaddr, senderAddress; bool socketMatch(sockaddr *first, sockaddr *second) { @@ -104,7 +108,7 @@ UDPSocket::UDPSocket(int listeningPort) { handle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (handle <= 0) { - printf("Failed to create socket.\n"); + printLog("Failed to create socket.\n"); return; } @@ -117,7 +121,7 @@ UDPSocket::UDPSocket(int listeningPort) { bind_address.sin_port = htons((uint16_t) listeningPort); if (bind(handle, (const sockaddr*) &bind_address, sizeof(sockaddr_in)) < 0) { - printf("Failed to bind socket to port %d.\n", listeningPort); + printLog("Failed to bind socket to port %d.\n", listeningPort); return; } @@ -127,7 +131,7 @@ UDPSocket::UDPSocket(int listeningPort) { tv.tv_usec = 500000; setsockopt(handle, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof tv); - printf("Created UDP socket listening on port %d.\n", listeningPort); + printLog("Created UDP socket listening on port %d.\n", listeningPort); } UDPSocket::~UDPSocket() { @@ -196,7 +200,7 @@ int UDPSocket::send(sockaddr *destAddress, const void *data, size_t byteLength) 0, (sockaddr *) destAddress, sizeof(sockaddr_in)); if (sent_bytes != byteLength) { - printf("Failed to send packet: %s\n", strerror(errno)); + printLog("Failed to send packet: %s\n", strerror(errno)); return false; } diff --git a/libraries/shared/src/shared_Log.cpp b/libraries/shared/src/shared_Log.cpp new file mode 100644 index 0000000000..ad8cd2d9c0 --- /dev/null +++ b/libraries/shared/src/shared_Log.cpp @@ -0,0 +1,17 @@ +// +// Log.cpp +// hifi +// +// Created by Tobias Schwinger on 4/17/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// + +#include "shared_Log.h" + +#include + +namespace shared { + using namespace std; + + int (* printLog)(char const*, ...) = & printf; +} diff --git a/libraries/shared/src/shared_Log.h b/libraries/shared/src/shared_Log.h new file mode 100644 index 0000000000..4916ddf5c1 --- /dev/null +++ b/libraries/shared/src/shared_Log.h @@ -0,0 +1,20 @@ +// +// shared_Log.h +// hifi +// +// Created by Tobias Schwinger on 4/17/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// + +#ifndef __hifi__shared_Log__ +#define __hifi__shared_Log__ + +namespace shared { + + // variable that can be set from outside to redirect the log output + // of this library + extern int (* printLog)(char const*, ...); +} + +#endif /* defined(__hifi__shared_Log__) */ +