升压试图第一个日志报表时登录导致崩溃(当不管理员) [英] Boost Log causes crash when trying first log statement (when not Administrator)

查看:160
本文介绍了升压试图第一个日志报表时登录导致崩溃(当不管理员)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想现在部署我的应用程序,它使用升压日志(升压1.58)。这是一个简单的控制台应用程序,中运行在Windows 7中记录工作完全正常我个人的桌面上。

I am trying to deploy my application right now, which uses Boost Log (Boost 1.58). It is a simple console app, being run in Windows 7. Logging works perfectly fine on my personal desktop.

然而,当我将应用程序部署到Win7的虚拟机,它崩溃在我的第一个日志语句:

However, when I deploy the application to a Win7 virtual machine, it crashes upon my first log statement:

boost::log::sources::severity_logger<SeverityLevel> slg;
BOOST_LOG_SEV(slg, SeverityLevel::Notification) << L"Application loaded"; // <-- Crash here

日志的目录的被创建,但日志文件永远不会被创建和应用程序崩溃。

The log directory gets created, but the log file never gets created and the application crashes.

我在我的文档目录试过我的%APPDATA%目录中的日志文件目录,而还。

I have tried a logfile directory in my %APPDATA% directory, and also in my My Documents directory.

奇怪的是:当我运行应用程序以管理员身份,它的工作原理的!

因此​​,这必须是一个权限的事情,但我有权限对这些文件夹,所以......

So this must be a permissions thing, but I have permissions to these folders, so...

任何想法?

*更多*

下面是code设置我的记录:

Here is the code to set up my logger:

    wstring appData = GetMyAppDataPath();
    boost::shared_ptr< boost::log::sinks::synchronous_sink< boost::log::sinks::text_file_backend > > textFileSink(new boost::log::sinks::synchronous_sink< boost::log::sinks::text_file_backend >(to store rotated files
        boost::log::keywords::file_name = "log_%7N.log",                // file name pattern
        boost::log::keywords::rotation_size = 16384                     // rotation size, in characters
        ));
    // Set up where the rotated files will be stored
    textFileSink->locked_backend()->set_file_collector(boost::log::sinks::file::make_collector(
        boost::log::keywords::target = appData + L"\\logs",         // where to store rotated files
        boost::log::keywords::max_size = 64 * 1024 * 1024,              // maximum total size of the stored files, in bytes (64 MiB)
        boost::log::keywords::min_free_space = 100 * 1024 * 1024        // minimum free space on the drive, in bytes
        ));
    // Upon restart, scan the target directory for files matching the file_name pattern
    textFileSink->locked_backend()->scan_for_files();
    // Set up the format for output to the text file.
    textFileSink->set_formatter(boost::log::expressions::stream
        << "[" << boost::log::expressions::attr< boost::posix_time::ptime >("TimeStamp") 
        << " " << boost::log::expressions::attr< SeverityLevel, severity_tag >("Severity") << "] "
        << boost::log::expressions::message
        );
    // Add it to the core
    boost::log::core::get()->add_sink(textFileSink);

HTTP:这主要是从这里取

://www.boost.org/doc/libs/1_58_0/libs/log/example/rotating_file/main.cpp

*也*

我加入增加了一个异常处理程序的加速记录仪:

I added an exception handler to the Boost Logger by adding:

boost::log::core::get()->set_exception_handler(boost::log::make_exception_handler<
        std::runtime_error,
        std::logic_error,
        std::exception
    >(pbb_boost_log_exception_handler()));

和然后加入处理程序。当我再运行,我能赶上以下异常飞机坠毁前:

And then adding the handler. When I then run, I am able to catch the following exception before the crash:

std::exception: Failed to open file for writing: Input/output error: "C:\Program Files\My App\log_0000000.log"

跆拳道?我绝对设置日志文件的位置到 APPDATA 价值,这一点我已经验证是正确的。此外,如果我运行这个应用程序作为管理员,日志文件在我希望它(应用程序数据文件夹)的地方结束。因此,它必须仅仅是在创建可执行文件的位置的临时文件。这是正常的行为呢?我无法想象这是...所以我做了什么?

WTF? I am definitely setting the log file location to the appData value, which I have verified is correct. Furthermore, if I run this app as administrator, the log file ends up in the place where I expect it (appdata folder). So it must just be creating a temporary file at the executable's location. Is this normal behavior? I can't imagine it is... so what did I do??

推荐答案

好吧,我想通了。

问题是这条线在这里,设置文本后端:

The issue was this line here, setting up the text backend:

boost::shared_ptr< boost::log::sinks::synchronous_sink< boost::log::sinks::text_file_backend > > textFileSink(new boost::log::sinks::synchronous_sink< boost::log::sinks::text_file_backend >
    boost::log::keywords::file_name = "log_%7N.log",                // file name pattern
    boost::log::keywords::rotation_size = 16384                     // rotation size, in characters
    ));

有肯定是暂时的日志文件被写入到本地目录(Program Files目录,这是坏的)。基于文档放在这里,我看到有的一个临时文件写的,然后传递到该文件收集器。

There was definitely a temporary log file being written to the local directory (a Program Files directory, which is bad). Based on the documentation here, I saw that there is a temporary file written, and then passed to the file collector.

因此​​,对于解决方案,我改变了code这样:

So for the solution, I changed the code to this:

boost::shared_ptr< boost::log::sinks::synchronous_sink< boost::log::sinks::text_file_backend > > textFileSink(new boost::log::sinks::synchronous_sink< boost::log::sinks::text_file_backend >(
    boost::log::keywords::file_name = appData + L"\\log_%7N.log",   // file name pattern
    boost::log::keywords::rotation_size = 16384                         // rotation size, in characters
    ));

请注意,我现在指定 FILE_NAME 作为AppData目录是代替。

Notice that I am now specifying the file_name as being in the AppData directory instead.

这个问题解决了

我也很难相信,我碰到这个问题,第一个,但我无法在网上找到这个的任何地方。这将是Windows开发人员常用反复出现的问题,所以希望这有助于别人。

I have a hard time believing that I am the first one to run into this issue, but I could not find this anywhere on the web. This would be a commonly recurring problem for windows developers, so hopefully this is helpful to someone else.

这篇关于升压试图第一个日志报表时登录导致崩溃(当不管理员)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆