Exemplo n.º 1
0
void CheckNSCP::handleLogMessage(const Plugin::LogEntry::Entry &message) {
	if (message.level() != Plugin::LogEntry_Entry_Level_LOG_CRITICAL && message.level() != Plugin::LogEntry_Entry_Level_LOG_ERROR)
		return;
	{
		boost::unique_lock<boost::timed_mutex> lock(mutex_, boost::get_system_time() + boost::posix_time::seconds(5));
		if (!lock.owns_lock())
			return;
		error_count_++;
		last_error_ = message.message();
	}
}
Exemplo n.º 2
0
			void simple_file_logger::do_log(const std::string data) {
				if (file_.empty())
					return;
				try {
					if (max_size_ != 0 && boost::filesystem::exists(file_.c_str()) && boost::filesystem::file_size(file_.c_str()) > max_size_) {
						int target_size = static_cast<int>(max_size_*0.7);
						char *tmpBuffer = new char[target_size + 1];
						try {
							std::ifstream ifs(file_.c_str());
							ifs.seekg(-target_size, std::ios_base::end);
							ifs.read(tmpBuffer, target_size);
							ifs.close();
							std::ofstream ofs(file_.c_str(), std::ios::trunc);
							ofs.write(tmpBuffer, target_size);
						} catch (...) {
							logger_helper::log_fatal("Failed to truncate log file: " + file_);
						}
						delete[] tmpBuffer;
					}
					if (!boost::filesystem::exists(file_.c_str())) {
						boost::filesystem::path parent = file_helpers::meta::get_path(file_);
						if (!boost::filesystem::exists(parent.string())) {
							try {
								boost::filesystem::create_directories(parent);
							} catch (...) {
								logger_helper::log_fatal("Failed to create directory: " + parent.string());
							}
						}
					}
					std::string date = nsclient::logging::logger_helper::get_formated_date(format_);

					Plugin::LogEntry message;
					if (!message.ParseFromString(data)) {
						logger_helper::log_fatal("Failed to parse message: " + str::format::strip_ctrl_chars(data));
					} else {
						std::stringstream tmp;
						for (int i = 0; i < message.entry_size(); i++) {
							Plugin::LogEntry::Entry msg = message.entry(i);
							tmp << date
								<< (": ") << utf8::cvt<std::string>(logger_helper::render_log_level_long(msg.level()))
								<< (":") << msg.file()
								<< (":") << msg.line()
								<< (": ") << msg.message() << "\n";
						}
						try {
							std::ofstream stream(file_.c_str(), std::ios::out | std::ios::app | std::ios::ate);
							if (!stream) {
								logger_helper::log_fatal(file_ + " could not be opened, Discarding: " + tmp.str());
							} else {
								stream << tmp.str();
							}
						} catch (std::exception &e) {
							logger_helper::log_fatal("Failed to write log: " + tmp.str() + ": " + e.what());
						}
					}
				} catch (std::exception &e) {
					logger_helper::log_fatal("Failed to parse data from: " + str::format::strip_ctrl_chars(data) + ": " + e.what());
				} catch (...) {
					logger_helper::log_fatal("Failed to parse data from: " + str::format::strip_ctrl_chars(data));
				}
			}