如何编写可写入/var/log/myapp目录的C/C ++应用程序? [英] How to write an C/C++ application that writes to a /var/log/myapp directory?

查看:68
本文介绍了如何编写可写入/var/log/myapp目录的C/C ++应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

在Linux系统上,/var/log的子目录中存在应用程序日志.由root/root拥有,并且在我的系统上具有755权限.例如,我看到/var/log/mysql/var/log/samba.

On Linux systems, Application Logs exist in subdirectories of /var/log, which is owned by root/root and has 755 permissions on my system. For example, I see /var/log/mysql and /var/log/samba.

问题

如果我希望 myapp 能够写入/var/log/myapp,在C/C ++中实现此目标的规范方式是什么?

If I want a myapp to be able to write into a /var/log/myapp, what is the canonical way of accomplishing this in C/C++?

想法

如果我不想sudo a_setup_script.sh,我是否必须做一些疯狂的事情,例如setuid root?请注意,我知道syslog例程,但是它们不足以满足我的需要(我需要记录更多信息,分为不同的文件,因此需要子目录).

Do I have to do something crazy like setuid root if I don't want to sudo a_setup_script.sh? Note that I am aware of the syslog routines, but they are insufficient for my needs (I need to log much more information, separated into different files, hence the need for the subdirectory).

我是否需要研究Ubuntu打包(以设置目录)并直接将文件IO导入子目录(通过 myapp )?

Do I need to look into a combination of Ubuntu packaging (to set up the directory) and direct file IO into the subdirectory (by myapp)?

我想尽可能地遵循标准.

I would like to follow standards as much as possible.

附录

我忘了提到了, myapp 实际上是一个守护进程(一个监听客户端的服务器),因此拥有 myapp_user 并不是一件坏事.运行/启动该过程.

I forgot to mention, myapp is actually a daemon processes (a server that listens to clients) so it wouldn't be so bad to have a myapp_user which actually runs/starts the process.

答案

对于Ubuntu,最好的解决方案似乎是rsyslog,它是syslog的强大,现代的替代品.它将根据需要生成文件/目录,它具有用于灵活路由syslog条目的内置语言,并且在C/C ++级别使用简单的旧syslog API.要存储路由信息,您可以在C/C ++中定义自己的文本消息编码,并结合适当的rsyslog.conf来处理解码.

For Ubuntu, the best solution appears to be rsyslog, a powerful, modern replacement for syslog. It will generate files/directories as necessary, it has a built-in language for flexible routing of syslog entries, and it uses the simple, old syslog API at the C/C++ level. To store routing information, you can define your own encoding of the text message in C/C++, in conjunction with a proper rsyslog.conf to handle the decoding.

推荐答案

不,不,不,不.这些东西没有suid.这些日志由称为"syslog"的进程管理,并且有一个API可以向此记录器发送消息:

No, no no no. No suid for such stuff. These logs are managed by a process known as "syslog" and there is an API to send messages to this logger:

   void openlog(const char *ident, int option, int facility);
   void syslog(int priority, const char *format, ...);
   void closelog(void);

或者您可以在命令行上输入"man syslog"并获取所有信息:-)

Or you can type 'man syslog' on the command line and get all the info :-)

更新:您将需要权限才能编辑syslog的配置文件,以将消息发送到单独的日志文件,否则它们将以默认位置(可能是/var/log/syslog)结束.

Update: you will need permissions to edit syslog's configuration file to send message to a separate log file, otherwise they will end up in the default location (probably /var/log/syslog).

这篇关于如何编写可写入/var/log/myapp目录的C/C ++应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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