Files
        @ 4ba86d2877e5
    
        
              Branch filter: 
        
    Location: libtransport.git/src/logging.cpp
        
            
            4ba86d2877e5
            4.0 KiB
            text/x-c++hdr
        
        
    
    ignore protoc output
    | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | /**
 * libtransport -- C++ library for easy XMPP Transports development
 *
 * Copyright (C) 2011, Jan Kaluza <hanzz.k@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 */
#include "transport/logging.h"
#include "transport/config.h"
#include <boost/foreach.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
#ifndef WIN32
#include "sys/signal.h"
#include <pwd.h>
#include <grp.h>
#include <sys/resource.h>
#include "libgen.h"
#else
#include <windows.h>
#include <process.h>
#define getpid _getpid
#endif
using namespace boost::filesystem;
namespace Transport {
namespace Logging {
#ifdef WITH_LOG4CXX
using namespace log4cxx;
static LoggerPtr root;
static void initLogging(Config *config, std::string key) {
	if (CONFIG_STRING(config, key).empty()) {
		root = log4cxx::Logger::getRootLogger();
#ifdef WIN32
		root->addAppender(new ConsoleAppender(new PatternLayout(L"%d %-5p %c: %m%n")));
#else
		root->addAppender(new ConsoleAppender(new PatternLayout("%d %-5p %c: %m%n")));
#endif
	}
	else {
		log4cxx::helpers::Properties p;
		log4cxx::helpers::FileInputStream *istream = NULL;
		try {
			istream = new log4cxx::helpers::FileInputStream(CONFIG_STRING(config, key));
		}
		catch(log4cxx::helpers::IOException &ex) {
			std::cerr << "Can't create FileInputStream logger instance: " << ex.what() << "\n";
		}
		catch (...) {
			std::cerr << "Can't create FileInputStream logger instance\n";
		}
		if (!istream) {
			return;
		}
		p.load(istream);
		LogString pid, jid;
		log4cxx::helpers::Transcoder::decode(boost::lexical_cast<std::string>(getpid()), pid);
		log4cxx::helpers::Transcoder::decode(CONFIG_STRING(config, "service.jid"), jid);
#ifdef WIN32
		p.setProperty(L"pid", pid);
		p.setProperty(L"jid", jid);
#else
		p.setProperty("pid", pid);
		p.setProperty("jid", jid);
#endif
		std::string dir;
		BOOST_FOREACH(const log4cxx::LogString &prop, p.propertyNames()) {
			if (boost::ends_with(prop, ".File")) {
				log4cxx::helpers::Transcoder::encode(p.get(prop), dir);
				boost::replace_all(dir, "${jid}", jid);
				break;
			}
		}
		if (!dir.empty()) {
			// create directories
			try {
				boost::filesystem::create_directories(
					boost::filesystem::path(dir).parent_path().string()
				);
			}
			catch (...) {
				std::cerr << "Can't create logging directory directory " << boost::filesystem::path(dir).parent_path().string() << ".\n";
			}
#ifndef WIN32
			if (!CONFIG_STRING(config, "service.group").empty() && !CONFIG_STRING(config, "service.user").empty()) {
				struct group *gr;
				if ((gr = getgrnam(CONFIG_STRING(config, "service.group").c_str())) == NULL) {
					std::cerr << "Invalid service.group name " << CONFIG_STRING(config, "service.group") << "\n";
				}
				struct passwd *pw;
				if ((pw = getpwnam(CONFIG_STRING(config, "service.user").c_str())) == NULL) {
					std::cerr << "Invalid service.user name " << CONFIG_STRING(config, "service.user") << "\n";
				}
				chown(dir.c_str(), pw->pw_uid, gr->gr_gid);
			}
#endif
		}
		log4cxx::PropertyConfigurator::configure(p);
	}
}
void initBackendLogging(Config *config) {
	initLogging(config, "logging.backend_config");
}
void initMainLogging(Config *config) {
	initLogging(config, "logging.config");
}
#else /* WITH_LOG4CXX */
void initBackendLogging(Config */*config*/) {
}
void initMainLogging(Config */*config*/) {
}
#endif /* WITH_LOG4CXX */
}
}
 |