Ejemplo n.º 1
0
/*
 * 打印日志
 *
 * @level	入参
 * @src_file	入参
 * @src_line	入参
 * @fmt		入参
 * @...		入参
 *
 * 返回:
 * 	0	成功
 * 	-1	open错
 * 	-2	配置错
 */
int Log(int level, char *src_file, int src_line, char *fmt, ...)
{
	int	fd;
	char	msg[1024 + 128] = {0};
	char	buf[1024 + 128] = {0};

	char	*pdate = DateNow(NULL);
	char	*ptime = TimeNow(NULL);
	va_list	args;

	switch(gnLogType)
	{
		case 0:
			fd = STDOUT_FILENO;
			break;
		case 1:
			memset(gsLogFile, 0, sizeof(gsLogFile));
			strcpy(gsLogFile, BuildFile("./log/logfile"));
			fd = open(gsLogFile, O_RDWR | O_APPEND | O_CREAT, 0644);
			if(fd == -1)
				return -1;
			break;
		default:
			return -2;
	}

	va_start(args, fmt);
	vsprintf(msg, fmt, args);
	va_end(args);

	sprintf(buf + strlen(buf), "[PID=%5d]", getpid());

	sprintf(buf + strlen(buf), "[%4s/", BufToStr(pdate, 4));
	sprintf(buf + strlen(buf), "%2s", BufToStr(pdate + 4, 2));
	sprintf(buf + strlen(buf), "/%2s]", BufToStr(pdate + 4, 2));
	
	sprintf(buf + strlen(buf), "[%2s", BufToStr(ptime, 2));
	sprintf(buf + strlen(buf), ":%2s", BufToStr(ptime + 2, 2));
	sprintf(buf + strlen(buf), ":%2s]", BufToStr(ptime + 4, 2));

//	sprintf(buf + strlen(buf), "[%15s: %04d]", src_file, src_line);

	if(strlen(buf) == 0)
		sprintf(buf, "%s\n", msg);
	else
		sprintf(buf + strlen(buf), " %s\n", msg);
	
	write(fd, buf, strlen(buf));
	
	if(fd != STDOUT_FILENO)
		close(fd);
	
	return 0;
}
ConfigurationReader::ConfigurationReader(string & _path)
{
	BuildFile(_path);
	COMPRESSION_VAL ="compression";
	METAFILE_NAME_VAL ="metafile";
	DATAFILE_NAME_VAL ="datafile",
		FILE_NAME_VAL = "filename";
	FOLDER_NAME_VAL = "datafolder";
	LOAD_TYPE_VAL = "loadtype";
	LOAD_TYPE_MULTI_VAL = "multiple";
	LOAD_TYPE_SINGLE_VAL = "single";
	LOAD_TYPE_CSV_VAL = "csv";
	SAVE_XML_FILE_FOLDER = "savedatafolder";
	DOUBLE_PRECISION_VAL = "precision";
	CSV_NULL_VAL = "csvnullval";
}
Ejemplo n.º 3
0
int main(int argc, char* argv[])
{
    // first collect files
    boost::filesystem::directory_iterator end;
    std::set<std::string> files;
    for (boost::filesystem::directory_iterator itr(boost::filesystem::current_path()); itr != end; ++itr)
        if (boost::filesystem::is_regular_file(itr->status()))
            if (itr->path().extension().string().find(".protoc") != std::string::npos)
                files.insert(itr->path().string());

    // its time to hack private members
    auto pool = google::protobuf::DescriptorPool::internal_generated_pool();
    void* mutex_ = *reinterpret_cast<char**>(pool);
    void* fallback_database_ = *(reinterpret_cast<void**>(pool) + 1);
    *reinterpret_cast<void**>(pool) = nullptr; // mutex_
    *(reinterpret_cast<void**>(pool) + 1) = nullptr; // fallback_database_

    std::vector<std::pair<google::protobuf::FileDescriptorProto, bool>> descProtos;
    std::set<std::string> parsed;
    std::list<std::string> parsedSorted;
    // then load everything into descriptor pool
    for (int i = 0; i < files.size(); ++i)
    {
        for (std::string const& fileName : files)
        {
            if (parsed.count(fileName))
                continue;

            std::ifstream in;
            in.open(fileName.c_str(), std::ios::binary | std::ios::in);
            if (!in.good())
                continue;

            google::protobuf::FileDescriptorProto fileDescProto;
            if (ParseFromIstreamWithDescriptorPool(fileDescProto, &in))
            {
                if (google::protobuf::FileDescriptor const* fileDesc = pool->BuildFile(fileDescProto))
                {
                    for (int i = 0; i < fileDesc->extension_count(); ++i)
                    {
                        google::protobuf::FieldDescriptor const* extension = fileDesc->extension(i);
                        switch (extension->type())
                        {
                            case google::protobuf::internal::WireFormatLite::TYPE_ENUM:
                                google::protobuf::internal::ExtensionSet::RegisterEnumExtension(
                                    google::protobuf::MessageFactory::generated_factory()->GetPrototype(extension->containing_type()),
                                    extension->number(),
                                    extension->type(),
                                    extension->is_repeated(),
                                    extension->is_packed(),
                                    [](int){ return true; });
                                break;
                            case google::protobuf::internal::WireFormatLite::TYPE_MESSAGE:
                            case google::protobuf::internal::WireFormatLite::TYPE_GROUP:
                                google::protobuf::internal::ExtensionSet::RegisterMessageExtension(
                                    google::protobuf::MessageFactory::generated_factory()->GetPrototype(extension->containing_type()),
                                    extension->number(),
                                    extension->type(),
                                    extension->is_repeated(),
                                    extension->is_packed(),
                                    google::protobuf::MessageFactory::generated_factory()->GetPrototype(extension->extension_scope()));
                                break;
                            default:
                                google::protobuf::internal::ExtensionSet::RegisterExtension(
                                    google::protobuf::MessageFactory::generated_factory()->GetPrototype(extension->containing_type()),
                                    extension->number(),
                                    extension->type(),
                                    extension->is_repeated(),
                                    extension->is_packed());
                                break;
                        }
                    }

                    parsed.insert(fileName);
                    parsedSorted.push_back(fileName);
                }
            }
        }
    }

    // and finally rebuild all protos in a new pool with fully resolved dependencies and extensions
    google::protobuf::DescriptorPool pool2;
    for (std::string const& fileName : parsedSorted)
    {
        std::ifstream in;
        in.open(fileName.c_str(), std::ios::binary | std::ios::in);
        if (!in.good())
            continue;

        google::protobuf::FileDescriptorProto fileDescProto;
        if (ParseFromIstreamWithDescriptorPool(fileDescProto, &in))
        {
            if (google::protobuf::FileDescriptor const* fileDesc = pool2.BuildFile(fileDescProto))
            {
                boost::filesystem::create_directories(boost::filesystem::path(fileDesc->name()).parent_path());
                std::ofstream f(fileDesc->name());
                f << fileDesc->DebugString() << std::endl;
                f.close();
            }
        }
    }

    // unhack to free memory
    *reinterpret_cast<void**>(pool) = mutex_;
    *(reinterpret_cast<void**>(pool) + 1) = fallback_database_;

    google::protobuf::ShutdownProtobufLibrary();

    return 0;
}