Apologies if this question has been asked before, but is there a built-in way to log debug output e.g. CCLOG() to file?
Thanks
Apologies if this question has been asked before, but is there a built-in way to log debug output e.g. CCLOG() to file?
Thanks
Thanks, but I was really after a built-in way of writing the cocos debug output to a file. I’m aware that it’s possible to write text files in C++
I’ve come up with the following solution for Windows, it’s maybe not the most elegant but it works. Add the following declaration to CCConsole.h around line 205:
void logToFile(const char* path);
…and this around line 296:
// file pointer for debug output
FILE* _debugLogFile;
Add the following code to CCConsole.h around line 717 overwriting the Console::log() function:
void Console::logToFile(const char* path)
{
_debugLogFile = fopen(path, "a");
std::string* str = new std::string("***********************************************\n");
time_t _tm = time(NULL);
struct tm* curtime = localtime(&_tm);
str->append("New session started at: ");
str->append(asctime(curtime));
str->append("***********************************************\n");
int ret = fwrite(str->c_str(), 1, strlen(str->c_str()), _debugLogFile);
}
void Console::log(const char* buf)
{
if( _sendDebugStrings ) {
_DebugStringsMutex.lock();
_DebugStrings.push_back(buf);
_DebugStringsMutex.unlock();
}
// write to file and flush write stream
if (_debugLogFile != NULL)
{
int ret = fwrite(buf, 1, strlen(buf), _debugLogFile);
if (ret > 0)
fflush(_debugLogFile);
}
}
Then in your game’s code use this to enable debugging to a file. Anything logged using CCLOG() will then output to the file. The path I’ve used ends up in the game’s .exe directory for me, but you can use anything you like:
auto console = Director::getInstance()->getConsole();
console->logToFile("..\\debug.log");
Hope this helps someone!!!
This doesnt really answer your question (as others have answered, I dont think there is a ‘built-in’ way)…but it might help someone with an idea:
I actually coded my own wrapper around CCLOG that also logs all the same CCLOG text to a large std::string. I happen to truncate that string to the last 20,000 chars. Then, I am able to make a CCLabel with that string, and output that string onto a CCLayer whenever I want (say, if the user taps their iPhone/Android with 3 fingers, or whatever), allowing me to show the CCLOG on any mobile device, which really helped me debug some stuff. Otherwise, the log only showed up when connected to a PC or Mac (obviously). Also, of course, it is then pretty easy to just write that string out to a file at any point. This is (probably) something you’d only want while debugging, and not have it ‘active’ during a release-to-store version.