openlog()\u3001syslog()\u3001closelog()\u3001\u662fLinux\u4e0b\u7528\u4e8e\u4e0esyslog\u670d\u52a1\u8fdb\u884c\u4ea4\u4e92\u7684\u51fd\u6570\u3002\u4e0b\u9762\u662f\u5b89\u88c5\u65b9\u6cd5\u548c\u4e00\u4e2a\u793a\u4f8b\uff1a\n\n1\u3002openlog()\u51fd\u6570\u7528\u4e8e\u5f00\u59cb\u4e0esyslog\u670d\u52a1\u7684\u8fde\u63a5\uff0c\u5e76\u8bbe\u7f6e\u4e00\u4e9b\u9009\u9879\u3002\n\nc\n#include <syslog.h>\n\nvoid openlog(const char *ident, int option, int facility);\n\n\n- ident\uff1a\u8bc6\u522b\u7b26\u53f7\uff0c\u5e38\u5e38\u662f\u7a0b\u5e8f\u7684\u540d\u79f0\uff0c\u5c06\u663e\u793a\u5728syslog\u6d88\u606f\u7684\u5934\u90e8\u3002\n- option\uff1a\u9009\u9879\uff0c\u53ef\u4ee5\u662fLOG_CONS\u3001LOG_NDELAY\u3001LOG_PERROR\u3001LOG_PID\u7b49\u4e4b\u4e00\uff0c\u53ef\u4ee5\u4f7f\u7528\u6309\u4f4d\u6216\u8fd0\u7b97\u7b26\u53f7\u7ec4\u5408\u591a\u4e2a\u9009\u9879\u3002\n- facility\uff1a\u8bbe\u65bd\uff0c\u8868\u793a\u6d88\u606f\u7684\u7c7b\u578b\uff0c\u5982LOG_USER\u3001LOG_MAIL\u3001LOG_DAEMON\u7b49\u3002\n\n2\u3002syslog()\u51fd\u6570\u7528\u4e8e\u5411syslog\u670d\u52a1\u53d1\u9001\u65e5\u5fd7\u6d88\u606f\u3002\n\nc\n#include <syslog.h>\n\nvoid syslog(int priority, const char *format, ...);\n\n\n- priority\uff1a\u4f18\u5148\u7ea7\uff0c\u5982LOG_INFO\u3001LOG_WARNING\u3001LOG_ERR\u7b49\u3002\n- format\uff1a\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u7c7b\u4f3c\u4e8eprintf\u51fd\u6570\u7684\u683c\u5f0f\u3002\n\n3\u3002closelog()\u51fd\u6570\u7528\u4e8e\u5173\u95ed\u4e0esyslog\u670d\u52a1\u7684\u8fde\u63a5\u3002\n\nc\n#include <syslog.h>\n\nvoid closelog(void);\n\n\n\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528openlog()\u3001syslog()\u548ccloselog()\u51fd\u6570\u5c06\u65e5\u5fd7\u6d88\u606f\u53d1\u9001\u5230syslog\u670d\u52a1\u5668\u7684\u793a\u4f8b\uff1a\n\nc\n#include <syslog.h>\n\nint main() {\n openlog("myprogram", LOG_PID, LOG_USER);\n \n syslog(LOG_INFO, "This is an informational message.");\n syslog(LOG_ERR, "This is an error message.");\n \n closelog();\n \n return 0;\n}\n\n\n\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528openlog()\u51fd\u6570\u5f00\u59cb\u4e0esyslog\u670d\u52a1\u7684\u8fde\u63a5\uff0c\u5e76\u8bbe\u7f6e\u8bc6\u522b\u7b26\u53f7\u4e3a"myprogram"\uff0c\u9009\u9879\u4e3aLOG_PID\uff08\u5305\u542b\u8fdb\u7a0bID\uff09\u548cLOG_USER\uff08\u6d88\u606f\u7c7b\u578b\u4e3a\u7528\u6237\u7ea7\u522b\uff09\u3002\n\n\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528syslog()\u51fd\u6570\u5411syslog\u670d\u52a1\u53d1\u9001\u4e00\u4e9b\u65e5\u5fd7\u6d88\u606f\u3002\u7b2c\u4e00\u4e2a\u6d88\u606f\u7684\u4f18\u5148\u7ea7\u4e3aLOG_INFO\uff0c\u5185\u5bb9\u4e3a"This is an informational message."\uff0c\u7b2c\u4e8c\u4e2a\u6d88\u606f\u7684\u4f18\u5148\u7ea7\u4e3aLOG_ERR\uff0c\u5185\u5bb9\u4e3a"This is an error message."\u3002\n\n\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528closelog()\u51fd\u6570\u5173\u95ed\u4e0esyslog\u670d\u52a1\u7684\u8fde\u63a5\u3002\n\n\u8fd9\u4e9b\u65e5\u5fd7\u6d88\u606f\u5c06\u88ab\u53d1\u9001\u5230syslog\u670d\u52a1\u5668\uff0c\u5e76\u6839\u636e\u4f18\u5148\u7ea7\u548c\u8bbe\u65bd\u8fdb\u884c\u5904\u7406\u548c\u8bb0\u5f55\u3002

Linux syslog() 函数使用指南:openlog()、syslog() 和 closelog() 示例

原文地址: https://www.cveoy.top/t/topic/qFzU 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录