提交 66823d2c 编写于 作者: J jp9000

Add logging to all operating systems

Also have it remove log files past 10 (default, changable with ini
setting as per usual).  10 might be too few.
上级 4f9e4a27
......@@ -15,6 +15,8 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include <time.h>
#include <stdio.h>
#include <sstream>
#include <util/bmem.h>
#include <util/dstr.h>
......@@ -28,36 +30,49 @@
#include "window-basic-main.hpp"
#include "platform.hpp"
#ifdef _WIN32
#include <fstream>
#ifdef _WIN32
#include <windows.h>
#define snprintf _snprintf
#else
#include <signal.h>
#endif
using namespace std;
#ifdef _WIN32
static log_handler_t def_log_handler;
static void do_log(int log_level, const char *msg, va_list args, void *param)
{
fstream &logFile = *static_cast<fstream*>(param);
char bla[4096];
vsnprintf(bla, 4095, msg, args);
char str[4096];
va_list args2;
va_copy(args2, args);
OutputDebugStringA(bla);
vsnprintf(str, 4095, msg, args);
#ifdef _WIN32
OutputDebugStringA(str);
OutputDebugStringA("\n");
#else
def_log_handler(log_level, msg, args2, nullptr);
#endif
if (log_level <= LOG_INFO)
logFile << bla << endl;
logFile << str << endl;
#ifdef _WIN32
if (log_level <= LOG_ERROR && IsDebuggerPresent())
__debugbreak();
}
#endif
}
bool OBSApp::InitGlobalConfigDefaults()
{
config_set_default_string(globalConfig, "General", "Language", "en");
config_set_default_uint(globalConfig, "General", "MaxLogs", 10);
#if _WIN32
config_set_default_string(globalConfig, "Video", "Renderer",
......@@ -81,18 +96,22 @@ static bool do_mkdir(const char *path)
static bool MakeUserDirs()
{
BPtr<char> configPath;
BPtr<char> path;
path = os_get_config_path("obs-studio");
if (!do_mkdir(path))
return false;
configPath = os_get_config_path("obs-studio");
if (!do_mkdir(configPath))
path = os_get_config_path("obs-studio/basic");
if (!do_mkdir(path))
return false;
configPath = os_get_config_path("obs-studio/basic");
if (!do_mkdir(configPath))
path = os_get_config_path("obs-studio/studio");
if (!do_mkdir(path))
return false;
configPath = os_get_config_path("obs-studio/studio");
if (!do_mkdir(configPath))
path = os_get_config_path("obs-studio/logs");
if (!do_mkdir(path))
return false;
return true;
......@@ -229,38 +248,113 @@ struct NoFocusFrameStyle : QProxyStyle
}
};
int main(int argc, char *argv[])
static void delete_oldest_log(void)
{
#ifndef WIN32
signal(SIGPIPE, SIG_IGN);
#endif
BPtr<char> logDir(os_get_config_path("obs-studio/logs"));
char firstLog[256] = {};
struct os_dirent *entry;
int ret = -1;
QCoreApplication::addLibraryPath(".");
#ifdef _WIN32
char *logPath = os_get_config_path("obs-studio/log.txt");
fstream logFile;
unsigned int maxLogs = (unsigned int)config_get_uint(
App()->GlobalConfig(), "General", "MaxLogs");
logFile.open(logPath, ios_base::in | ios_base::out | ios_base::trunc,
_SH_DENYNO);
bfree(logPath);
os_dir_t dir = os_opendir(logDir);
if (dir) {
unsigned int count = 0;
if (logFile.is_open())
while ((entry = os_readdir(dir)) != NULL) {
if (entry->directory)
continue;
/* no hidden files */
if (*entry->d_name == '.')
continue;
if (!*firstLog)
strncpy(firstLog, entry->d_name, 255);
count++;
}
os_closedir(dir);
if (count > maxLogs && *firstLog) {
stringstream delPath;
delPath << logDir << "/" << firstLog;
os_unlink(delPath.str().c_str());
}
}
}
static void create_log_file(fstream &logFile)
{
stringstream dst;
time_t now = time(0);
struct tm *cur_time;
cur_time = localtime(&now);
if (cur_time) {
char file[256] = {};
snprintf(file, sizeof(file), "%d-%02d-%02d %02d-%02d-%02d.txt",
cur_time->tm_year+1900,
cur_time->tm_mon+1,
cur_time->tm_mday,
cur_time->tm_hour,
cur_time->tm_min,
cur_time->tm_sec);
dst << "obs-studio/logs/" << file;
BPtr<char> path(os_get_config_path(dst.str().c_str()));
logFile.open(path,
ios_base::in | ios_base::out | ios_base::trunc);
}
if (logFile.is_open()) {
delete_oldest_log();
base_set_log_handler(do_log, &logFile);
#endif
} else {
blog(LOG_ERROR, "Failed to open log file");
}
}
static int run_program(fstream &logFile, int argc, char *argv[])
{
int ret = -1;
QCoreApplication::addLibraryPath(".");
try {
OBSApp program(argc, argv);
OBSTranslator test;
program.installTranslator(&test);
OBSTranslator translator;
create_log_file(logFile);
program.installTranslator(&translator);
program.setStyle(new NoFocusFrameStyle);
program.OBSInit();
ret = program.exec();
} catch (const char *error) {
blog(LOG_ERROR, "%s", error);
}
return ret;
}
int main(int argc, char *argv[])
{
#ifndef WIN32
signal(SIGPIPE, SIG_IGN);
#endif
base_get_log_handler(&def_log_handler, nullptr);
fstream logFile;
int ret = run_program(logFile, argc, argv);
blog(LOG_INFO, "Number of memory leaks: %ld", bnum_allocs());
base_set_log_handler(nullptr, nullptr);
return ret;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册