google-glogã«æ½ãããªãã¯ã解æãã
ãgoogle-glogã¯é常ã«æåãªãã®ã³ã°ã©ã¤ãã©ãªã§ããããã®ååãããããéãgoogleã®äººã ã«ãã£ã¦éçºããã¦ããã使ãæ¹ã¯ç°¡åã§ã
LOG(INFO) << "this is not a drill";
ãã¿ãããªæãã§ãLOG()ãè¿ããªãã¸ã§ã¯ãoperator<<ã§è¨é²ããããªãã¸ã§ã¯ããã¤ãªãã¦ããã ãã§ä½¿ãããã¨ã¦ã便å©ã§ãããå®ã¯ããã®ä¾¿å©ãã®è£ã«ã¯ãå®ã¯ããã¤ãã®ããªãã¯ãé ãã¦ãããé©å½ã«è¦ãç®ãçä¼¼ãã¦ä½ãã ãã§ã¯ãglogã¨åããããªä¾¿å©ãã¯å®ç¾ã§ããªãã®ã§ãããä»æ¥ã¯ããã®ä¾¿å©ããå®ç¾ãã¦ããããªãã¯ãç´¹ä»ãããã
ãªãLOG()ã¯ãã¯ããªã®ã
ãã¾ãããã®LOG(INFO)ã¨ããã®ã¯ä¸è¦ãé¢æ°ãããã¯ã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ããªã«ãã«è¦ãããããããã®å®ä½ã¯å®ã¯ãã¯ãã§ã以ä¸ã®ããã«å±éãããã
LogMessage(INFO).stream()
ãLOGã¨ããã¯ã©ã¹ãããããã§ã¯ãªããããã®ã¯LogMessageåã®ã¯ã©ã¹ã¨ãstreamã¡ã½ãããè¿ãLogStreamåã®ã¯ã©ã¹ã®2ã¤ã§ãããããããã®å½¹å²ãç°¡åã«èª¬æããããã¾ããLogMessageã¯ã©ã¹ã§ããããå®ã¯ããã¤ã¯å¤§ããå½¹ç®ããã£ã¦ããªããåã«LogStreamã¯ã©ã¹ã®ã¡ã³ããæã¡ããããstreamã¡ã½ããã§è¿ãã¦ããã ãã§ããã
ãå®éã«å½¹ã«ç«ã¤å¦çãè¡ãªã£ã¦ããã®ã¯LogStreamã¯ã©ã¹ã®æ¹ã§ãããããã¯ãããã¡çãªãªãã¸ã§ã¯ãã§ãoperator<<ã§ä¸ããããã¡ãã»ã¼ã¸ããããã¡ã«æºãè¾¼ã¿ããã¹ãã©ã¯ã¿ã§ãã°ç¨ã®streamã«ä¸æ°ã«æ¸ãåºããLogStreamåãªãã¸ã§ã¯ãããªããã¹ãã©ã¯ã¿ã§æ¸ãåºããè¡ãã®ãã¯å¾ã§èª¬æããã¨ãã¦ãã¾ãã¯ãªããã®ãããªåå²æ§æã«ãªã£ã¦ããã®ãããã®ä½æ
ã«è¿«ã£ã¦ãããã
ããã®åå²æ§æã ã¨ãLogMessageã¯ã©ã¹ã¯å¤§ãããã¨ããã¦ããªãã¦ãä¸è¦ãLogStreamã¯ã©ã¹ã ããããã°ããã§ããããã«è¦ãããããããªãã¨ãLOGã¨ãããã¯ãã¯å¥ã«å¿
è¦ãªãã¦ãLogStreamã¯ã©ã¹ãLOGã¨ããã¯ã©ã¹åã§å®ç¾©ããã°ããããã«æããããããããå®ã¯ãã®ããã«ä½ã£ã¦ã¿ãã¨ããã¾ããããªããoperator<<ã¯ãã®ãããªä½¿ãæ¹ã許ãã¦ããªãã®ã ã
ãLOGãã¯ã©ã¹ã«ããå ´åãLOG(INFO)ã¯ä¸æãªãã¸ã§ã¯ããè¿ããã¨ã«ãªããããããã¨ãLOG(INFO) << "a"; ã¿ãããªå¼åºã¯ãoperator<<(LOG &log, const char *s) ã¿ãããªã·ã°ããã£ã«ãªãã®ã ããããã§ç¬¬ä¸å¼æ°ã®LOG &logã«constãã¤ãã¦ããªãã®ãã¾ãããä¸æãªãã¸ã§ã¯ãã¯é¢æ°ã®å¼æ°ã«æ¸¡ãã¨ãã«ã¯ãå¤ã§æ¸¡ããããããã¯conståç
§ã«ãã渡ããªãã®ã§ããã
ãä¸æãªãã¸ã§ã¯ããconståç
§ã«ãã渡ããªãã®ã¯ãªãããã¾ã æªã ã«çç±ãããããããªãã¨ããããå¾å¿ããããªãããã©ãããããããã®ããããhttp://q.hatena.ne.jp/1167111891 ã¨ãã«ããã®ãããªè©±é¡ãããã
ããã¦ãããã¾ã§æ¸ãã¨ãglogãã©ãããããªãã¯ã§åé¡ã解決ãã¦ããã®ããè¦ãã¦ãããã®ã¨æããããLogMessageã¯ã©ã¹ã¨LogStreamã¯ã©ã¹ããããã¦ããã®ã¯ãLogMessageã¯ã©ã¹ãä½ã£ãä¸æãªãã¸ã§ã¯ãã¯conståç
§ã«ãã渡ããªãã®ã ãããã®ã¡ã½ããã®æ»ãå¤ã«ã¯ãç¹ã«ãããã£ãå¶éã¯ã¤ãã¦ããªãããã§ãããã¤ã¾ããLogMessageã¯ã©ã¹ãåå¨ããçç±ã¯ãåç´ã«conståç
§ã®å¶éãåé¿ããããã§ããã
ããã®ãããªæ§è³ªã®ãã¨ããC++ã®constã¯æ¨ç§»çã§ã¯ãªããã¨è¡¨ç¾ãããã¨ãããããã®ãããã¯Dè¨èªã®è§£èª¬ã®æ¹ã詳ãããâHere A Const, There A Const
ãconstãªãã¸ã§ã¯ãã®ã¡ã³ããç¹ã«constã§ã¯ãªããã¨ããæ§è³ªãå©ç¨ããã®ã¯ãèªã¿ã«ããã³ã¼ããçãåå ã¨ãªãå¾ãã®ã§ãã¾ãæã¾ããäºã§ã¯ãªããä»åã¯ä»ã«ããæ¹ããªãã®ã§ä»æ¹ãªãããããæ段ãåã£ã¦ããã¨èãã¦ããã¹ãã§ããã¾ãèªåã®æ¸ãã³ã¼ãã§æ¿«ç¨ãã¦ã¯ãããªãé¡ã®æ§è³ªã§ããäºã¯ãä¸ç·ã«è¦ãã¦ããã¹ãã ããã
ãªããã¹ãã©ã¯ã¿ã§ãã°ãæ¸ãåºãã®ã
ãå ç¨ã®ã³ã¼ãã®ä¾ãããä¸åº¦ã ããã
LOG(INFO) << "this is not a drill";
ãä¸ã®ã³ã¼ãã¯ãä¸ã®ããã«å±éãããã
LogMessage(INFO).stream() << "this is not a drill";
ãå®éã«ã¯LogMessage_INFO.stream()ã¿ããã«ãªãã®ã ããããã¯ä»ã¯ãã¾ãéè¦ã§ã¯ãªãã®ã§ç½®ãã¦ããã
ãstreamã¡ã½ãããè¿ãã®ã¯LogStreamåã®ãªãã¸ã§ã¯ãã§ããããã®ãªãã¸ã§ã¯ãã¯operator<<ã§åãåã£ãå¤ããããã¡ã«æºãè¾¼ãã§ããã¹ãã©ã¯ã¿ã§ä¸æ°ã«ãã¡ã¤ã«ã¸ã¨æ¸ãåºãã
ããªããã®ãããªä»æ§ã«ãªã£ã¦ããã®ãã¨ããã¨ãåããã¡ã¤ã«ã¸è¤æ°ã¹ã¬ããããæ¸ãåºããè¡ãéã«ããã°ã®è¡ãæ··ãã£ã¦ãã¾ããã¨ãé²ãããã§ããã
LOG(INFO) << "A" << "B";
ãã¿ãããªè¡ãè¤æ°ã®ã¹ã¬ããã§åæã«å®è¡ããå ´åã"AABB"ã¿ããã«ãã°ãæ¸ãåºããã¦ãã¾ããã¨ãé²ãããã«ã¯ããããã°è¡ãæ§æããæååã確å®ãã¦ããä¸æ°ã«æ¸ãåºããè¡ããããªãããããã°è¡ã§ã©ãããæååãæ¸ãåºãããã®ãã決å®ãã段éã§å¼ã³åºãããã³ã¼ãããã¹ãã©ã¯ã¿ããæ®ã£ã¦ããªãããããããããããªãã®ã§ããã
ããããä¸ã®ããªãã¯ã¨åãã§ãæ¬æ¥ã¯ãã¾ã使ãã¹ãã§ã¯ãªãããæ¹ã§ããããã¹ãã©ã¯ã¿ã§ã¯å¤±æããå¯è½æ§ã®ããæä½ã¯ã§ããã ãé¿ããã¹ãã ããã¹ãã©ã¯ã¿ã§ã¯ä¾å¤ãæããã¹ãã§ã¯ãªãã®ã§ã失æããã¨ãã¦ãã失æãããã¨ãéç¥ããæ¹æ³ããªãããã§ããã
- ãã¹ãã©ã¯ã¿ã®ç´åã§å¼ã°ãã
- ä¾å¤ã«ããã¹ã¿ãã¯å·»æ»ãã®éã«ã¯å¼ã°ããªã
ããããªã¡ã½ããããªãã¸ã§ã¯ãã«ç»é²ã§ããæ©è½ãããã°ããã®ãããªæ©è½ãããé©åãªå½¢ã§å®ç¾ã§ãããã ããç¾å¨ã®C++ã®ä»æ§ã§ã¯ãã¹ãã©ã¯ã¿ã§ãã¡ã¤ã«ã¸ã®æ¸ãåºããè¡ããããªãã
ã¾ã¨ã
ãglogã§ã¯ã
- C++ã§ã¯ä¸æãªãã¸ã§ã¯ããconståç §ã«ãã渡ããªãã®ã§ãLogMessageã¨LogStreamã®2ã¤ã«ã¯ã©ã¹ãåå²ããå¿ è¦ããã
- ãã«ãã¹ã¬ãã対å¿ã®ããã«ããã¹ãã©ã¯ã¿ã§ãã°ãæ¸ãè¾¼ãããã«ãªã£ã¦ãã
ãå®éã®google-glogã¯å®ã¯ããã¡ãã£ã¨ãã¯ãã«ã¾ã¿ãã¦ãããããè¤éã«ãªã£ã¦ããã®ã ããã ããã以ä¸ã®ãããªãã¨ãç解ããã°ãgoogle-glogã®ã³ã¼ããèªããã¨ãã§ããã¯ãã§ãããglogã®ã³ã¼ããèªãã ã®ã¯1ã¶æ以ä¸åã®äºã§ãä»ã¯ä¸å¿ã¡ããã¡ããã½ã¼ã¹ã³ã¼ãã¯åç
§ããªããã¨ã³ããªãæ¸ãã¦ãããã©ãå³å¯ã«ç¢ºèªããªããæ¸ãé²ãã¦ããããã§ã¯ãªãã®ã§ãããããããééããå«ãã§ãããããããªãããããã£ããã¨ãäºè§£ããä¸ã§ããã²å®éã®glogã®ã³ã¼ãã«è§¦ãã¦ããã ãããã
ããã£ã¨ããglogã«ã¯åç´ãªãã¬ã¼ã¨ãã¦ã®å½¹å²ã ãã§ãªããsegmentation faultã§è½ã¡ãéã«ããã¯ãã¬ã¼ã¹ããã°ã«åãã¨ããæ©è½ãããããã¡ãã®æ¹ãèªãããã«ã¯ãããé«åº¦ãªç¥èãå¿
è¦ã¨ãªãããã®è¾ºã¯ã¾ãä»åº¦ãã¾ãå
æ°ãåºãã¨ãã«èª¿ã¹ã¦ã¿ããã¨æãã
ãã¾ãï¼ãã¹ãã©ã¯ã¿ã§ä¾å¤ãæãã¦ã¯ãããªãã®ãï¼
ãgoogle-glogã¯ãã¹ãã©ã¯ã¿ã§ä¾å¤ãæãã¦ã¯ããªãã¨æããããã®ãããªãã¬ã¼ãä½ãã¨ãã¹ãã©ã¯ã¿ã§ä¾å¤ãæããããªããããããªãã
ãã§ãããã¹ãã©ã¯ã¿ã§ã¯ä¾å¤ãæãã¦ã¯ãããªãã¨æããããã©ãããããããã®ã ãããã
ãããããããªããã¹ãã©ã¯ã¿ã§ä¾å¤ãæãã¦ã¯ãããªãã®ãã¨ããã¨ãä¾å¤ãçºçãã¦ã¹ã¿ãã¯å·»æ»ãã«ãã£ã¦ãã¹ãã©ã¯ã¿ãå¼ã°ããéã«ããã§ããã«ãã¹ãã©ã¯ã¿ãä¾å¤ãæããããã©ããã«éæ¾ãããªãè³æºãæ®ã£ã¦ãã¾ãå¯è½æ§ãé«ãããã§ããã
ãã¨ãããã¨ã¯ã
- ç¾å¨ãã¹ã¿ãã¯å·»ãæ»ãä¸ã§ãªããã¨ããã¹ãã©ã¯ã¿ã®ä¸ã§å¤æã§ãã
- ã³ã³ã¹ãã©ã¯ã¿ãããã¹ãã©ã¯ã¿ã®éã§ä¾å¤ãçºçãããã¨ããªãã¨ä¿è¨¼ã§ãã
ãã®ã©ã¡ããã®æ¡ä»¶ãæºãããã®ã§ããã°ããã¹ãã©ã¯ã¿ããä¾å¤ãæãã¦ãæ§ããªãããã ã
ãåè
ã«é¢ãã¦ã¯ããã¼ã¿ãã«ã«å®è£
ããã®ã¯ã©ããä¸å¯è½ã§ããããããã¨ãããã¨ã調ã¹ãçµæããã£ãããã¼ã¿ãã«ã§ãªãã¦ãä¸å¯è½ãããããªãã
ãå¾è
ã«é¢ãã¦ã¯ãä¿è¨¼ã¯ã§ããªãã®ã ããå®é¨ãã¦ã¿ãã¨ããããã®ãã¬ã¼ã®ã±ã¼ã¹ã§ã¯ã©ãã大ä¸å¤«ããã§ãããä¾ãã°ããã¬ã¼ã«ä»¥ä¸ã®ãããªã³ã¼ãã渡ãã¦ããå®è¡é åºçã«ã¯åé¡ãªãã£ããï¼ãã¹ãã¯g++4.4ã§è¡ãªã£ã¦ãããï¼ãã ããããã¯ãããã¾ã§ã1ã¤ã®ã³ã³ãã¤ã©ã§ãã¾ãã¾ãããªã£ãã¨ããã ãã®è©±ã§ãã£ã¦ãè¦æ ¼æ¸ã«æ¸ãã¦ããããã§ã¯ãªãããã ã
class A { public: int i_; A(int i) :i_(i) { cout << "constructor for A" << endl; } }; ostream & operator<< (ostream &os, const A &a) { os << a.i_; return os; } int main(int argc, char *argv[]) { LOG(INFO) << A(3); }
ãçµè«ã¨ãã¦ã¯ããªãããã¹ãã©ã¯ã¿ã§ä¾å¤ãæãã¦ããã®å ´åã«ã¯å¤§ä¸å¤«ããã ããä¸è¬çã«æãã¦æ§ããªãã¨ã¯å
¨ç¶è¨ããªããã¨ããã¨ããã ããããã©ããç
®ããããªããã©ã
ãç¹ã«ãªãã¨ãã¯ãªãã®ã ããC++ã¯é£ããã§ããã