Simple C++ logger by using singleton pattern

前端 未结 4 1925
旧巷少年郎
旧巷少年郎 2021-02-09 17:28

Due to the flooding examples of implementing logger using Singleton pattern, I have just written a simple C++ logger in the same approach for my program. However, since the famo

4条回答
  •  梦谈多话
    2021-02-09 17:47

    In applications with threads, I prefer to use singletons with an initialize() function and asserts to make sure that the initialize() is used before the first instance(). Call initialize() from the main thread. I don't think that lazy instantiation is really the key feature of a singleton, especially for a logger.

    While Arkaitz's answer is more elegant, my answers avoids threading issues on all platforms with the cost of 1 extra function and some instantiating concerns during startup for singletons with dependencees (helped by asserts and ofcourse: use singletons judiciously).

提交回复
热议问题