Annotation of nono/host/logger.h, revision 1.1.1.2

1.1       root        1: //
                      2: // nono
                      3: // Copyright (C) 2025 nono project
                      4: // Licensed under nono-license.txt
                      5: //
                      6: 
                      7: //
                      8: // ログ
                      9: //
                     10: 
                     11: #pragma once
                     12: 
                     13: #include "thread.h"
                     14: #include <condition_variable>
                     15: #include <deque>
                     16: #include <mutex>
                     17: #include <thread>
                     18: 
                     19: class TextScreen;
                     20: 
                     21: // ログは世界が出来る前から(全部終わった最後まで)機能していてほしいため、
                     22: // ThreadDevice からの継承はしない。
                     23: // ログウィンドウ用のモニタを持つために Object を継承しているが、
                     24: // ログは出力できない。
                     25: class Logger : public Object, public ThreadBase
                     26: {
                     27:        using inherited = Object;
                     28: 
                     29:        // ログの最大行数。modulo するので 2 のべき乗推奨。
                     30:        static const uint maxlines = 64;
                     31: 
                     32:        static constexpr uint32 REQ_QUEUE               = 0x00000001;
                     33:        static constexpr uint32 REQ_TERMINATE   = 0x80000000;
                     34: 
                     35:  public:
                     36:        Logger();
                     37:        ~Logger() override;
                     38: 
                     39:        // スレッドを開始する。
                     40:        bool StartThread();
                     41: 
                     42:        // UTF-8 -> Shift_JIS 変換関数を登録する。
                     43:        void SetConverter(std::string (*)(const std::string&));
                     44: 
                     45:        // スレッドに終了を要求し、その終了を待つ。
                     46:        void TerminateThread();
                     47: 
                     48:        // 標準出力にも出すかどうか
                     49:        void UseStdout(bool val) { use_stdout = val; }
                     50: 
                     51:        // ログの表示桁数を変更する。
                     52:        bool ResizeCol(int);
                     53: 
                     54:        // ログ書き込み。
                     55:        void Write(const char *);
                     56: 
                     57:        // 表示バッファの有効な行数を返す。
                     58:        int GetDispLines() const { return displines; }
                     59: 
                     60:        Monitor *GetMonitor() const { return monitor; }
                     61: 
                     62:  private:
1.1.1.2 ! root       63:        void OnStart();
1.1       root       64:        void Terminate();
                     65:        void ThreadRun();
                     66:        void DoLog(const std::string&);
                     67:        void AddLog(const std::string&);
                     68:        void AddDisplay(const std::string&);
                     69: 
                     70:        DECLARE_MONITOR_CALLBACK(MonitorUpdate);
                     71: 
                     72:        // ログ受け付けキュー。
                     73:        std::deque<std::string> queue {};
                     74: 
                     75:        // ログ受け付けキューの条件変数。
                     76:        uint32 request {};
                     77:        std::mutex queue_mtx {};
                     78:        std::condition_variable cv {};
                     79: 
                     80:        // 標準出力にも出すかどうか
                     81:        bool use_stdout {};
                     82: 
                     83:        // バックログの実体。
                     84:        std::deque<std::string> logs {};
                     85: 
                     86:        // 表示用に構成した中間バッファ。
                     87:        // 横が col 桁 * 高さ maxlines 行の固定長。
                     88:        // 行単位のリングバッファとして使う。
                     89:        // そのまま表示用になるので、空いてるところは ' '(空白) で埋める。
                     90:        std::vector<uint8> dispbuf {};
                     91: 
                     92:        // 表示バッファの桁数。
                     93:        uint col {};
                     94: 
                     95:        // 次に書き込む行 (== 最古の行)。dispbuf の先頭を 0行目と数える。
                     96:        int current {};
                     97: 
                     98:        // 表示バッファの有効な行数。
                     99:        int displines {};
                    100: 
                    101:        // UTF-8 -> Shift_JIS 変換関数。(GUI が登録する)
                    102:        std::string (*utf8_to_sjis_converter)(const std::string&);
                    103: 
                    104:        Monitor *monitor {};
                    105: 
                    106:        // バックログのロック。
                    107:        std::mutex backend_mtx {};
                    108: 
                    109:        // スレッド。
                    110:        std::unique_ptr<std::thread> thread {};
                    111: 
                    112:        // スレッド開始同期用
                    113:        std::mutex thread_starter {};
                    114: };

unix.superglobalmegacorp.com

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