Annotation of nono/lib/logger.cpp, revision 1.1.1.5

1.1       root        1: //
                      2: // nono
1.1.1.3   root        3: // Copyright (C) 2020 nono project
                      4: // Licensed under nono-license.txt
1.1       root        5: //
                      6: 
1.1.1.5 ! root        7: //
        !             8: // ログ
        !             9: //
1.1       root       10: 
1.1.1.5 ! root       11: #include "logger.h"
1.1       root       12: 
                     13: // コンストラクタ
                     14: Logger::Logger()
                     15: {
                     16: }
                     17: 
                     18: // デストラクタ
                     19: Logger::~Logger()
                     20: {
                     21: }
                     22: 
                     23: // ログ書き込み
                     24: void
                     25: Logger::Write(const char *str)
                     26: {
1.1.1.5 ! root       27:        std::lock_guard<std::mutex> lk(mtx);
1.1       root       28:        // パイプへ出力 (EOL まで出力する)
                     29:        for (const char *s = str; ; s++) {
1.1.1.4   root       30:                if (pipe.IsFull()) {
                     31:                        // 一杯なら古い 1 行を捨てる
                     32:                        while (pipe.Dequeue() != '\0') {
                     33:                                // nop
                     34:                        }
1.1       root       35:                }
1.1.1.4   root       36:                pipe.Enqueue(*s);
                     37:                if (*s == '\0') {
1.1       root       38:                        break;
1.1.1.4   root       39:                }
1.1       root       40:        }
                     41:        // 必要なら標準出力にも出力
                     42:        if (use_stdout) {
                     43:                printf("%s\n", str);
                     44:        }
                     45: }
                     46: 
                     47: // ログ読み出し
                     48: bool
                     49: Logger::Read(char *buf, int bufsize)
                     50: {
                     51:        uint8 ch;
                     52:        int i;
                     53: 
1.1.1.5 ! root       54:        std::lock_guard<std::mutex> lk(mtx);
1.1       root       55:        for (i = 0; i < bufsize - 1 && pipe.Dequeue(&ch); ) {
                     56:                buf[i++] = ch;
                     57:                if (ch == '\0') {
                     58:                        break;
                     59:                }
                     60:        }
                     61:        // buf が足りない場合ここで打ち切り
                     62:        if (i == bufsize - 1) {
                     63:                // ただし EOL 含めて bufsize ぴったりなら取り出しておく
                     64:                if (pipe.Peek(0) == '\0') {
                     65:                        pipe.Dequeue(&ch);
                     66:                }
                     67:                buf[i] = '\0';
                     68:        }
                     69:        return (i != 0);
                     70: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.