在mac OS-X多线程项目中使用asl布局记录
Logging with asl layout on mac OS-X multi-threaded project
我想转换我所有的日志消息在我的多线程项目,使用苹果系统日志设施(或asl)。
根据以下asl手册- https://developer.apple.com/library/ios/documentation/System/Conceptual/ManPages_iPhoneOS/man3/asl_get.3.html
当从多个线程记录日志时,每个线程必须使用asl_open打开一个单独的客户端句柄。
因此,我为每个线程定义了一个客户端,以便在所有日志命令中使用。但是,在将asl客户端绑定到每个asl_log命令时面临一些主要困难。
1. what if some of my asl log commands reside in a code that is common for
more than one thread - which asl client should i decide use on such message.
2. Even on thread unique code, one should be consistent in choosing the same
asl_client on all log functions on a single thread code scope (this is
not always easy to find in complex projects.).
是否有更简单的方法来采用我的项目日志消息使用asl ?
我会考虑将一个客户端绑定到线程,
谢谢
好的,所以到目前为止我发现的最好的解决方案是创建一个全局变量asl客户端,它是线程特定的。
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <asl.h>
#define NUMTHREADS 4
pthread_key_t glob_var_key;
void print_func() //take global var and use it as the aslclient per thread
{
asl_log(*((aslclient*) pthread_getspecific(glob_var_key)),NULL,ASL_LEVEL_NOTICE, "blablabla");
}
void* thread_func(void *arg)
{
aslclient *p = malloc(sizeof(aslclient));
// added tid to message format to distinguish between messages
uint64_t tid;
pthread_threadid_np(NULL, &tid);
char tid_str[20];
sprintf(tid_str, "%llu", tid);
*p = asl_open(tid_str,"Facility",ASL_OPT_STDERR);
pthread_setspecific(glob_var_key, p);
print_func();
sleep(1); // enable ctx switch
print_func();
pthread_setspecific(glob_var_key, NULL);
free(p);
pthread_exit(NULL);
}
int main(void)
{
pthread_t threads[NUMTHREADS];
int i;
pthread_key_create(&glob_var_key,NULL);
for (i=0; i < NUMTHREADS; i++)
pthread_create(&threads[i],NULL,thread_func,NULL);
for (i=0; i < NUMTHREADS; i++)
pthread_join(threads[i], NULL);
}
相关文章:
- 如何使用C/C++在MacOSX中获得键盘布局
- Vulkan验证层不断在VkQueuePresentKHR()上抛出图像布局错误
- 布局兼容类型的并集
- Qt自定义QPush按钮未显示在布局上
- 按钮悬停在 QT 中垂直布局的选项卡小部件中不起作用
- 调整布局上的 QGraphicsView 小部件的大小
- 如何在qt中将对象添加到现有布局中?--已解决
- 将布局映射到内存地址
- C++继承的虚拟类的内存布局
- 检查nullptr是否100%保护内存布局不受segfault影响
- C++ Python 的扩展 - 安全内存访问和内存布局
- QTree查看新行,没有布局就不可见已更改
- 类似元组的类模板的反向内存布局
- C++对象布局是否必须静态定义?
- 集合布局上的 Qt 分割错误
- QT将图像从缩略图拖放到网格布局(QVTKOpenGLWidget)?
- 更正GLSL无绑定纹理句柄中的结构布局
- POD类型是否完全等同于琐碎的标准布局类型
- 聚合类型是否意味着它也是标准布局
- 在mac OS-X多线程项目中使用asl布局记录