From f00263c60f709291af9d40b239c423e4488247d5 Mon Sep 17 00:00:00 2001 From: paulc Date: Wed, 4 May 2011 14:53:03 +0000 Subject: [PATCH] Use open() instead of fopen() for CDR files so O_LARGEFILE can be specified. Made tabs format default and removed override from sample config file. git-svn-id: http://voip.null.ro/svn/yate@4357 acf43c95-373e-0410-b603-e72c3f656dc1 --- conf.d/cdrfile.conf.sample | 6 +++--- modules/cdrfile.cpp | 38 ++++++++++++++++++++++++-------------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/conf.d/cdrfile.conf.sample b/conf.d/cdrfile.conf.sample index 4785cf30..b217309f 100644 --- a/conf.d/cdrfile.conf.sample +++ b/conf.d/cdrfile.conf.sample @@ -1,11 +1,11 @@ [general] ; file: string: Name of the file to write the CDR to +; You should check that this file is log rotated - see /etc/logrotate.d/yate +; Example: file=/var/log/yate-cdr.tsv ;file= -file=/tmp/cdr.tsv ; tabs: bool: Use tab-separated instead of comma-separated if format is missing -;tabs=false -tabs=true +;tabs=true ; format: string: Custom format to use, overrides default. Each ${parameter} ; is replaced with the value of that parameter in the call.cdr message diff --git a/modules/cdrfile.cpp b/modules/cdrfile.cpp index 3e8d26a1..6f61133b 100644 --- a/modules/cdrfile.cpp +++ b/modules/cdrfile.cpp @@ -24,7 +24,11 @@ #include -#include +#include +#include +#include +#include +#include using namespace TelEngine; namespace { // anonymous @@ -34,36 +38,43 @@ class CdrFileHandler : public MessageHandler, public Mutex public: CdrFileHandler(const char *name) : MessageHandler(name), Mutex(false,"CdrFileHandler"), - m_file(0) + m_file(-1) { } virtual ~CdrFileHandler(); virtual bool received(Message &msg); void init(const char *fname, bool tabsep, const char* format); private: - FILE *m_file; + int m_file; String m_format; }; CdrFileHandler::~CdrFileHandler() { Lock lock(this); - if (m_file) { - ::fclose(m_file); - m_file = 0; + if (m_file >= 0) { + ::close(m_file); + m_file = -1; } } void CdrFileHandler::init(const char *fname, bool tabsep, const char* format) { Lock lock(this); - if (m_file) - ::fclose(m_file); + if (m_file >= 0) { + ::close(m_file); + m_file = -1; + } m_format = format; if (m_format.null()) m_format = tabsep ? "${time}\t${billid}\t${chan}\t${address}\t${caller}\t${called}\t${billtime}\t${ringtime}\t${duration}\t${direction}\t${status}\t${reason}" : "${time},\"${billid}\",\"${chan}\",\"${address}\",\"${caller}\",\"${called}\",${billtime},${ringtime},${duration},\"${direction}\",\"${status}\",\"${reason}\""; - m_file = fname ? ::fopen(fname,"a") : 0; + if (fname) { + m_file = ::open(fname,O_WRONLY|O_CREAT|O_APPEND|O_LARGEFILE,0640); + if (m_file < 0) + Debug(DebugWarn,"Failed to open or create '%s': %s (%d)", + fname,::strerror(errno),errno); + } } bool CdrFileHandler::received(Message &msg) @@ -75,16 +86,15 @@ bool CdrFileHandler::received(Message &msg) return false; Lock lock(this); - if (m_file && m_format) { + if ((m_file >= 0) && m_format) { String str = m_format; str += "\n"; msg.replaceParams(str); - ::fputs(str.safe(),m_file); - ::fflush(m_file); + ::write(m_file,str.c_str(),str.length()); } return false; }; - + class CdrFilePlugin : public Plugin { public: @@ -117,7 +127,7 @@ void CdrFilePlugin::initialize() Engine::install(m_handler); } if (m_handler) - m_handler->init(file,cfg.getBoolValue("general","tabs"),cfg.getValue("general","format")); + m_handler->init(file,cfg.getBoolValue("general","tabs",true),cfg.getValue("general","format")); } INIT_PLUGIN(CdrFilePlugin);