Category: C++

  • 创建单件模式的Logger类

    《C++高级编程》第二十六章的一个例子。
    /**
    * Logger.h
    *
    * Definition of a singleton logger class, implemented with static methods
    */
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <string>
    class Logger
    {
    public:
    static const std::string kLogLevelDebug;
    static const std::string kLogLevelInfo;
    static const std::string kLogLevelError;
    // Return a reference to the singleton logger object
    static Logger& instance();
    // Log a single message at the given log level
    void log(const std::string& inMessage, const std::string& inLogLevel);
    // Log a vector of messages at the given log level
    void log(const std::vector<std::string>& inMessage, const std::string& inLogLevel);
    protected:
    // static variable for the one-and-only instance
    static Logger sInstance;
    // Constant for the filename
    static const char* const kLogFileName;
    // Data member for the output stream
    std::ofstream mOutputStream;
    private:
    // 关键:私有的构造函数,不能实例化,因为实例化时类外部无法访问其内部的私有的构造函数
    Logger(void);
    ~Logger(void);
    };

    /**
    * Logger.cpp
    *
    * Implementation of a singleton logger class
    */
    #include “Logger.h”
    #include <string>
    using namespace std;
    const string Logger::kLogLevelDebug = “DEBUG”;
    const string Logger::kLogLevelInfo = “INFO”;
    const string Logger::kLogLevelError = “Error”;
    const char* const Logger::kLogFileName = “log.out”;
    // The static instance will be construct when the program starts and destructed when it ends.
    Logger Logger::sInstance;
    Logger::Logger(void)
    {
    mOutputStream.open(kLogFileName, ios_base::app);
    if (!(mOutputStream.good())) {
    cerr << “Unable to initialize the Logger!” << endl;
    }
    }
    Logger::~Logger(void)
    {
    mOutputStream.close();
    }
    Logger& Logger::instance()
    {
    return sInstance;
    }
    void Logger::log(const std::string& inMessage, const std::string& inLogLevel)
    {
    mOutputStream << inLogLevel << “:” << inMessage << endl;
    }
    void Logger::log(const std::vector<std::string>& inMessage, const std::string& inLogLevel)
    {
    for (size_t i=0; i<inMessage.size(); i++) {
    log(inMessage[i], inLogLevel);
    }
    }

    // TestTrueSingletonLogger.cpp
    #include “Logger.h”
    #include <vector>
    #include <string>
    int main(int argc, char **argv)
    {
    Logger::instance().log(“test message”, Logger::kLogLevelDebug);
    vector<string> items;
    items.push_back(“item1”);
    items.push_back(“item2”);
    Logger::instance().log(items, Logger::kLogLevelError);
    return 0;
    }

  • Freetds 连接SQL Server 错误:Unexpected EOF from the server

    安装

    安装好freetds 0.82后,用tsql连接数据库,会出现以下错误:

    locale is "en_US.UTF-8"
    locale charset is "UTF-8"
    using default charset "UTF-8"
    Error 20017 (severity 9):
            Unexpected EOF from the server
            OS error 115, "Operation now in progress"
    Error 20002 (severity 9):
            Adaptive Server connection failed

    There was a problem connecting to the server

    程序连接也一样,Unexpected EOF from the server。

    查看网上的内容,可能与配置有关,于是修改配置文件freetds.conf,将对应的内容修改为以下的值:

    tds version = 8.0

    再次连接,成功连接到SQL Server。

  • gcc编译文件名后缀不匹配错误

    用gcc编译一个xx.cpp的文件或出现如下错误:
    /tmp/ccq5K20D.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0′
    用g++可以编译,如果要用gcc编译,改边文件后缀为*.c

  • ACE WSA Startup not initialized 问题

    将属性->配置属性->常规(Gerneral)
    字符集(Character Set): Use Unicode Character Set
    改为
    Character Set:未设置( Not Set)
    这样就ok了
    默认ACE使用多字节字符集
    如果你想ACE使用 Unicode 字符集,
    需要添加下面两行
    #define ACE_HAS_WCHAR
    #define ACE_USE_WCHAR

  • C++/Java命名规则

    –from 《Code Complete》
    ClassName 类名混合使用大小写,首字母大写
    TypeName 类型定义,包括枚举类型和typedef,混合使用大小写,首字母大写
    EnumeratedTypes 除遵循上述规则外,枚举类型总以复数形式表示
    localVariable 局部变量混合使用大小写,首字母小写。其名字应该与底层数据类型无关,而且应
    该反映该变量所代表的事物
    routineParameters 子程序参数的格式与局部变量相同
    RoutineName() 子程序名混合使用大小写
    m_ClassVariable 对类的多个子程序可见(且只对该类可见)的成员变量使用m_前缀
    g_GlobalVariable 全局变量用g_前缀
    CONST 具名变量全部大写
    MACRO 宏全部大写
    Base_EnumeratedType 枚举类型名用能反映其基础类型的、单数形式的前缀,例如Color_Red
    好的子程序名:
    描述子程序所做的所有的事情;避免使用无意义的、模糊或表述不清的动词;不要仅通过数字来形成不同的子程序名;根据需要确定子程序名长度;给函数命名时要对其返回值有所描述;给过程起名时要使用语气强烈的动词加宾语的形式