- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个广泛使用 boost log 2.0 的应用程序。现在我想为该应用程序设置一些默认标志,如 std::setprecision(std::numeric_limits<double>::digits10 + 1)
、 std::scientific
和 std::left
。但是我该怎么做呢?一种方法是在我的主要功能的最开始创建一个记录器并创建一个虚拟日志消息。这将永久设置所需的标志。但是没有更好的方法来做到这一点吗?
编辑回复:“OP should show actual code.”
我有一个全局日志记录单例,称为 L:
class L{
public:
enum severity_level
{
dddebug,
ddebug,
debug,
control,
iiinfo,
iinfo,
info,
result,
warning,
error,
critical
};
typedef boost::log::sources::severity_channel_logger<
severity_level, // the type of the severity level
std::string // the type of the channel name
> logger_t;
typedef boost::log::sinks::synchronous_sink< boost::log::sinks::text_ostream_backend > text_sink;
boost::shared_ptr< text_sink > sink_;
static L& get();
static boost::shared_ptr<text_sink> sink();
static double t0();
static double tElapsed();
private:
L();
double t0_p;
static std::string tElapsedFormat();
L(const L&) = delete;
void operator=(const L&) = delete;
};
它提供了一个日志接收器、严重级别并利用 MPI 方法在 MPI 节点之间进行同步计时。类成员的实现如下:
#include "log.h"
#include <iomanip>
#include <limits>
#include <fstream>
#include <boost/log/attributes/function.hpp>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/smart_ptr/make_shared_object.hpp>
#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/sources/severity_channel_logger.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
namespace logging = boost::log;
namespace src = boost::log::sources;
namespace expr = boost::log::expressions;
namespace sinks = boost::log::sinks;
namespace attrs = boost::log::attributes;
namespace keywords = boost::log::keywords;
#include "mpiwrap.h"
#include <mpi.h>
BOOST_LOG_ATTRIBUTE_KEYWORD(t, "Time", std::string)
BOOST_LOG_ATTRIBUTE_KEYWORD(rank, "Rank", int)
BOOST_LOG_ATTRIBUTE_KEYWORD(channel, "Channel", std::string)
BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", L::severity_level)
L::L():
sink_(boost::make_shared< text_sink >()),
t0_p(MPI_Wtime())
{
sink_->locked_backend()->add_stream(
boost::make_shared< std::ofstream >("log." + std::to_string(MpiWrap::getRank())));
sink_->set_formatter
(
expr::stream
<< "< "
<< t << " "
<< "[p:" << rank << "] "
<< "[c:" << channel << "] "
<< "[s:" << severity << "] "
<< expr::smessage
);
logging::core::get()->add_sink(sink_);
logging::core::get()->set_filter(
(channel == "ChannelName1" && severity >= dddebug)
|| (channel == "ChannelName2" && severity >= info)
|| (channel == "ChannelName3" && severity >= result)
);
// Add attributes
logging::core::get()->add_global_attribute("Time", attrs::make_function(&tElapsedFormat));
logging::core::get()->add_global_attribute("Rank", attrs::constant<int>(MpiWrap::getRank()));
}
L& L::get(){
static L instance;
return instance;
}
boost::shared_ptr<L::text_sink> L::sink(){
return get().sink_;
}
double L::t0(){
return get().t0_p;
}
double L::tElapsed(){
return MPI_Wtime() - t0();
}
std::string L::tElapsedFormat(){
std::stringstream ss;
const int prec = std::numeric_limits<double>::digits10;
ss << std::setw(prec + 2 + 6) << std::left << std::setprecision(prec) << tElapsed();
return ss.str();
}
std::ostream& operator<< (std::ostream& strm, L::severity_level level)
{
static const char* strings[] =
{
"DBG3",
"DBG2",
"DBG1",
"CTRL",
"INF3",
"INF2",
"INF1",
"RSLT",
"WARN",
"ERRR",
"CRIT"
};
if (static_cast< std::size_t >(level) < sizeof(strings) / sizeof(*strings))
strm << strings[level];
else
strm << static_cast< int >(level);
return strm;
}
现在开始使用:我的类通常有一个静态 logger_t
(boost::log::sources::severity_channel_logger<severity_level, std::string>
的类型定义)成员
class A {
public:
logger_t logger;
//other stuff here
void function_which_does_logging();
};
L::logger_t A::logger(boost::log::keywords::channel = "ClassA");
void A::function_which_does_logging(){
//do non logging related stuff
BOOST_LOG_SEV(logger, L::result) << "the error is: " << 0.1234567890;
//do non logging related stuff
}
我目前对这个问题的解决方案是在我的程序开头放置一个日志语句
int main(){
L::logger_t logger(boost::log::keywords::channel = "init");
BOOST_LOG_SEV(logger, L::critical) << "setting up logger" << std::scientific << std::setprecision(std::numeric_limits<double>::digits10 + 1);
//do stuff
}
最佳答案
@rhashimoto 很好地说明了您当前的解决方案将如何因多线程/并发日志记录操作而崩溃。我觉得最好的解决方案是定义您自己的日志记录宏来替换包含流修饰符的 BOOST_LOG_SEV
,如下所示:
#define LOG_SCIENTIFIC(logger, sev) (BOOST_LOG_SEV(logger, sev) << std::scientific)
它可以用作 BOOST_LOG_SEV
的替代品,后者将数字格式化为科学数据。但是,检查您的代码并用新的自定义宏替换每个日志记录操作可能会很痛苦。除了定义自己的宏之外,您还可以重新定义 BOOST_LOG_SEV
以按照您的意愿运行。 boost/log/sources/severity_feature.hpp
定义 BOOST_LOG_SEV
如下:
//! An equivalent to BOOST_LOG_STREAM_SEV(logger, lvl)
#define BOOST_LOG_SEV(logger, lvl) BOOST_LOG_STREAM_SEV(logger, lvl)
因为 BOOST_LOG_STREAM_SEV
仍然是公共(public) boost API 的一部分,您应该能够像这样安全地重新定义 BOOST_LOG_SEV
:
#define BOOST_LOG_SEV(logger, lvl) (BOOST_LOG_STREAM_SEV(logger, lvl) << std::scientific)
只要它是在包含 boost 日志 header 之后定义的,它就应该执行您想要的操作。但是,我建议使用具有自定义名称的宏,这样其他人就可以清楚地知道您的代码在做什么。
关于c++ - 如何在 boost log 2.0 中设置 std::ios_base 标志,如 std::left?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23087376/
不同的 LogCat 方法是: Log.v(); // Verbose Log.d(); // Debug Log.i(); // Info Log.w(); // Warning Log.e();
在android群里,经常会有人问我,android log是怎么用的,今天我就把从网上以及sdk里东拼西凑过来,让大家先一睹为快,希望对大家入门android log有一定的帮助. android
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 4 年前。 社区 12
我正在尝试使用 sonarlint 检查代码质量.上面的问题概要,我不明白为什么它要说要大写。但是 this discussion与上述建议相反。哪一个应该被认为是正确的? 最佳答案 这没有正确答案,
随着 n 变大,log*(log n) 和 log(log* n) 这两个函数会更快吗? 这里,log* 函数是迭代对数,定义如下: 我怀疑它们是相同的,只是写法不同,但它们之间有什么区别吗? 最佳答
作为家庭作业,我被要求在 O(log(n)) 中编写一个算法,我可以计算出我编写的算法的复杂度为 O(log(n) + log(n/2) + log(n/4) + log(n/8) + ... + l
我正在使用 Tomee。日志文件夹包含这样的文件 localhost_access_log.2016-12-02.txt localhost.2016-12-02.log catalina.2016-
Android Log.v、Log.d、Log.i、Log.e 等的 ios 等效项是什么?同样在 android 上,我使用 Android 设备监视器和 logcat 来访问我的手机日志,我需要在
我认为下面的代码是 O(log log n) 因为它里面有 i*i 但我对 log n 感到困惑> 和 log (log n)。 for (i=2; i*i<=number; i++) { if
我正在修改 kvm 模块,并在内核代码中添加了 printk 语句。运行虚拟机后,printk 为我提供了错误地址和有关 guest 操作系统的其他信息。 我需要从这个信息中生成统计信息。当我使用 d
我有一个部署为 Windows Azure Web 角色的 WCF 服务。 我正在使用 Enterprise Library 进行异常处理,并且在我的本地 Development Fabric 中,似
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 9 年前。 Improv
在 Go 的生产中使用 log.SetFlags(log.LstdFlags | log.Lshortfile) 是好的做法(至少是一般做法)吗?我想知道在生产中这样做是否存在性能或安全问题。因为它不
我想知道什么更快: double value = Math.log(a) - Math.log(b); 或 double value = Math.log(a/b); 我计算值的方式是否会对性能产生影
我有数百个子例程使用 log.Println() 写入日志文件 我正在使用 log.Println 写入 error.log 文件。 func main() { e, err := os.Open
我将 Nuxt 与 SSR 一起使用,并希望有类似于 apaches 的 access.log 和 error.log 的东西 我特别感兴趣的是每次调用的响应时间。 我在 nuxt 文档中找不到任何内
我知道以前有人问过这个问题,但我相信这是一个不同的问题。 Nginx 在 www-data 下运行: $ ps -eo "%U %G %a" | grep nginx root root
我在我的日志文件中发现了一个非常奇怪的条目 Jan 29 01:35:30 vs-proj-handy sshd[5316]: Received disconnect from 130.207.203
对于我正在开发的应用程序,我希望在开发过程中和发布时简化故障排除。我希望能够检索到对 Log 的调用,以了解在 USB 调试中没有连接手机的情况下运行应用程序时的调用,以便可以检索并发送给我。例如,当
我试图捕获 panic 并记录错误: func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloRep
我是一名优秀的程序员,十分优秀!