Skip to content

Instantly share code, notes, and snippets.

@taxilian
Created October 15, 2011 04:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taxilian/1289031 to your computer and use it in GitHub Desktop.
Save taxilian/1289031 to your computer and use it in GitHub Desktop.
Example of adding a file-based logging method
#include <boost/filesystem.hpp>
#include "SystemHelpers.h"
using namespace boost::filesystem;
/// ...
void getLoggingMethods( FB::Log::LogMethodList& outMethods )
{
path appDataPath = FB::System::getLocalAppDataPath("CompanyName");
path logDirPath = appDataPath / "logs";
if (exists(logDirPath) && is_directory(logDirPath)) {
std::stringstream ss;
time_t seconds = time(NULL);
boost::thread::id threadId = boost::this_thread::get_id();
ss << seconds << "_" << threadId << ".log";
path logPath = logDirPath / ss.str();
fprintf(stderr, "logging to %s", logPath.string().c_str());
outMethods.push_back(std::make_pair(FB::Log::LogMethod_File, logPath.string()));
}
#ifndef NDEBUG
outMethods.push_back(std::make_pair(FB::Log::LogMethod_Console, std::string()));
#endif
}
// ...
@taxilian
Copy link
Author

Note that this requires add_boost_library(filesystem) in your PluginConfig (because it relies on boost::filesystem). If you use this exactly as written logging to the console will be enabled for all debug builds and logging to file will be enabled if the directory exists.

On windows 7 this will be in the %USERPROFILE%\AppData\LocalLow\CompanyName\logs dir, on xp it will be %USERPROFILE%\Application Data\Local Settings\CompanyName\logs, and on Mac it'll be ~/Library/Application Support/CompanyName/logs

@TaylorBoon
Copy link

Also need to #include "boost/thread.hpp" if it's not already present (can't add angles in comments it seems).

@kangear
Copy link

kangear commented Oct 27, 2019

how about linux ?

@kangear
Copy link

kangear commented Oct 27, 2019

got it, the reason was *.so has not be update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment