std::cout\std::endl通过头文件宏定义重载、自己实现std::cout

libLog.hpp

#include 
#include 
#include 
#include 
#include 

#define LOGCPP_MYCOUT_MAX_LENTH (10)    

namespace LIBLOGCPP {
class Cout
    {
        private:
            std::string str;
            std::mutex mtx;
        public:
            Cout ()
            {
                printf("[start]\n");
            }
            ~Cout ()
            {
                printf("\n[end]\n");
            }
            void checkLength(void)
            {
                if (str.length() >= LOGCPP_MYCOUT_MAX_LENTH) {
                    printf("%s\n", str.c_str());
                    str.clear();
                }
            }
            Cout &operator<<(char *msg)
            {
                //printf("[my]%s", msg);
                mtx.lock();
                str += msg;
                checkLength();
                mtx.unlock();
                return *this;
            }
            Cout &operator<<(const char *msg)
            {
                //printf("[my]%s", msg);
                mtx.lock();
                str += msg;
                checkLength();
                mtx.unlock();
                return *this;
            }
            Cout &operator<<(std::string &msg)
            {
                //printf("[my]%s", msg.c_str());
                mtx.lock();
                str += msg;
                checkLength();
                mtx.unlock();
                return *this;
            }
            Cout &operator<<(unsigned char ch)
            {
                //printf("[my]%c", ch);
                mtx.lock();
                str += ch;
                checkLength();
                mtx.unlock();
                return *this;
            }
            Cout &operator<<(unsigned short u16)
            {
                //printf("[my]%u", u16);
                mtx.lock();
                str += std::to_string(u16);
                checkLength();
                mtx.unlock();
                return *this;
            }
            Cout &operator<<(unsigned int u32)
            {
                //printf("[my]%u", u32);
                mtx.lock();
                str += std::to_string(u32);
                checkLength();
                mtx.unlock();
                return *this;
            }
            Cout &operator<<(unsigned long u64)
            {
                //printf("[my]%llu", u64);
                mtx.lock();
                str += std::to_string(u64);
                checkLength();
                mtx.unlock();
                return *this;
            }
            Cout &operator<<(unsigned long long u64)
            {
                //printf("[my]%llu", u64);
                mtx.lock();
                str += std::to_string(u64);
                checkLength();
                mtx.unlock();
                return *this;
            }
            Cout &operator<<(char ch)
            {
                //printf("[my]%c", ch);
                mtx.lock();
                str += ch;
                checkLength();
                mtx.unlock();
                return *this;
            }
            Cout &operator<<(short i16)
            {
                //printf("[my]%d", i16);
                mtx.lock();
                str += std::to_string(i16);
                checkLength();
                mtx.unlock();
                return *this;
            }
            Cout &operator<<(int i32)
            {
                //printf("[my]%d", i32);
                mtx.lock();
                str += std::to_string(i32);
                checkLength();
                mtx.unlock();
                return *this;
            }
            Cout &operator<<(long i64)
            {
                //printf("[my]%lld", i64);
                mtx.lock();
                str += std::to_string(i64);
                checkLength();
                mtx.unlock();
                return *this;
            }
            Cout &operator<<(long long i64)
            {
                //printf("[my]%lld", i64);
                mtx.lock();
                str += std::to_string(i64);
                checkLength();
                mtx.unlock();
                return *this;
            }
            Cout &operator<<(float f64)
            {
                //printf("[my]%lld", i64);
                mtx.lock();
                str += std::to_string(f64);
                checkLength();
                mtx.unlock();
                return *this;
            }
            Cout &operator<<(double d64)
            {
                //printf("[my]%lld", i64);
                mtx.lock();
                str += std::to_string(d64);
                checkLength();
                mtx.unlock();
                return *this;
            }
            //实现endl
            Cout &operator << (std::ostream& (*__pf)(std::ostream&))
            {
                mtx.lock();
                str += "endl\n";
                printf("%s", str.c_str());
                str.clear();
                mtx.unlock();
                return *this;
            }        
    };

//    inline Cout cout;// 方式1:c++11不支持
//    static Cout cout;//方式2:每个.c都要实例化一个变量,内存消耗大
}

//libLog.cpp中定义
/*
	#include "libLogCpp.h"
	LIBLOGCPP::Cout gCout;
*/
extern LIBLOGCPP::Cout gCout; //方式3:引用一个全局变量

#define COUT LIBLOGCPP::cout

main.cpp

int main() {
    std::string echostring;
    uint8_t a = 1;
    uint16_t b = 2;
    uint32_t c = 3;
    uint64_t d = 4;
    int8_t e = -1;
    int16_t f = -2;
    int32_t g = -3;
    int64_t h = -4;
    float i = 1.0;
    double j = 2.1;

    echostring = "std::string;";
    COUT << "test;" << "hello;" << echostring << a << b << c << d << e << f << g << h << i << j << std::endl;
    COUT << "test;" << "hello;" << echostring << a << b << c << d << e << f << g << h << i << j << std::endl;

    return 0;
}

输出


[start]
test;hello;
std::string;
1234-1-2-3
-41.000000
2.100000endl
test;hello;
std::string;
1234-1-2-3
-41.000000
2.100000endl

[end]

你可能感兴趣的:(c++,算法,开发语言)