示例#1
0
    // writes the common header info to the stream
    void LogMessage::Init(const char* file, int line)
    {
        // log only the filename
        const char* last_slash = strrchr(file, '\\');
        if(last_slash)
        {
            file = last_slash + 1;
        }

        stream_ <<  '[';
        if(log_process_id)
        {
            stream_ << CurrentProcessId() << ':';
        }
        if(log_thread_id)
        {
            stream_ << CurrentThreadId() << ':';
        }
        if(log_timestamp)
        {
            time_t t = time(NULL);
            struct tm local_time = { 0 };
            localtime_s(&local_time, &t);
            struct tm* tm_time = &local_time;
            stream_ << std::setfill('0')
                << std::setw(2) << 1 + tm_time->tm_mon
                << std::setw(2) << tm_time->tm_mday
                << '/'
                << std::setw(2) << tm_time->tm_hour
                << std::setw(2) << tm_time->tm_min
                << std::setw(2) << tm_time->tm_sec
                << ':';
        }
        if(log_tickcount)
        {
            stream_ << TickCount() << ':';
        }
        stream_ << log_severity_names[severity_] << ":" << file <<
            "(" << line << ")] ";

        message_start_ = stream_.tellp();
    }
示例#2
0
//	////////////////////////////////////////////////////////////////////////////
void LogHandlerFileBase::OpenFile(const char *base_name, const char *dir_name,
	const MLB::Utility::TimeT &start_time)
{
	std::string file_name;

	if ((base_name == NULL) || (!(*base_name)))
		file_name = "LogFile.";
	else {
		file_name = base_name;
		if (base_name[strlen(base_name) - 1] != '.')
			file_name += '.';
	}

	std::string tmp_date_time(start_time.ToString());

	tmp_date_time[10]  = '.';
	tmp_date_time[13]  = '.';
	tmp_date_time[16]  = '.';
	file_name         += tmp_date_time + "." + GetHostNameCanonical() + "." +
		AnyToString(CurrentProcessId()) + ".log";

	boost::filesystem::path tmp_file(file_name, boost::filesystem::native);

	if ((dir_name != NULL) && *dir_name) {
		std::string tmp_dir_name;
		ResolveFilePathGeneral(dir_name, tmp_dir_name, "", true, true, false);
		boost::filesystem::path tmp_path(tmp_dir_name, boost::filesystem::native);
		boost::filesystem::path this_file;
		this_file        = tmp_path / tmp_file;
		file_name        = this_file.native_file_string();
	}
	else {
		if (!tmp_file.has_root_path())
			tmp_file = boost::filesystem::system_complete(tmp_file);
		file_name = tmp_file.native_file_string();
	}

	OpenFile(file_name);
}