|
|
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.7 root 7: //
1.1.1.8 root 8: // 基本オブジェクト
1.1.1.7 root 9: //
1.1.1.8 root 10: // オブジェクトはログ出力機能 (とそのための自身のオブジェクト名) を持つ。
11:
1.1.1.10! root 12: #include "nono.h"
1.1.1.8 root 13: #include "object.h"
14: #include "logger.h"
15: #include "mainapp.h"
16:
1.1 root 17: // コンストラクタ
1.1.1.10! root 18: Object::Object(uint objid_)
1.1 root 19: {
1.1.1.9 root 20: objid = objid_;
1.1.1.10! root 21: assertmsg(objid < idname.size(),
! 22: "objid=%u, size=%zu", objid, idname.size());
1.1.1.9 root 23:
24: // オブジェクト名(ログ表示名)は ID から引く。
25: objname = idname[objid];
1.1.1.7 root 26:
27: // コンストラクタで指定した場合はエイリアスにも登録しておく。
1.1.1.9 root 28: // ただし OBJ_NONE ならログを出すことも想定していないはずなので
29: // エイリアスも追加しない。
30: if (objid != OBJ_NONE) {
31: AddAlias(objname);
32: }
1.1 root 33:
34: // 自分自身をリストに繋ぐ
1.1.1.9 root 35: gMainApp.RegistObject(this);
1.1 root 36: }
37:
38: // デストラクタ
39: Object::~Object()
40: {
1.1.1.7 root 41: // 自分自身をリストから削除する
1.1.1.9 root 42: gMainApp.UnregistObject(this);
1.1.1.8 root 43: }
44:
45: // ログレベルを設定する。
46: void
47: Object::SetLogLevel(int loglevel_)
48: {
49: loglevel = loglevel_;
1.1 root 50: }
51:
52: // メッセージ表示
53: // 常に表示 (あらかじめ loglevel の評価をした後で呼び出す用)
54: void
1.1.1.6 root 55: Object::putmsgn(const char *fmt, ...) const
1.1 root 56: {
1.1.1.2 root 57: char buf[1024];
1.1.1.6 root 58: va_list ap;
1.1.1.8 root 59: int len;
1.1 root 60:
1.1.1.7 root 61: // 名前だけ生表示されても分かりづらいので括弧でもつけておく
62: len = snprintf(buf, sizeof(buf), "(%s) ", GetName().c_str());
1.1.1.8 root 63:
1.1.1.6 root 64: va_start(ap, fmt);
1.1.1.2 root 65: vsnprintf(buf + len, sizeof(buf) - len, fmt, ap);
1.1.1.6 root 66: va_end(ap);
67:
1.1.1.8 root 68: WriteLog(buf);
1.1 root 69: }
70:
1.1.1.8 root 71: // ログ表示 (継承側で用意すること)
1.1 root 72: void
1.1.1.6 root 73: Object::putlogn(const char *fmt, ...) const
1.1 root 74: {
1.1.1.8 root 75: PANIC("Object::putlogn called");
76: }
1.1 root 77:
1.1.1.8 root 78: // ロガーに出力する共通部分
79: void
80: Object::WriteLog(const char *buf) const
81: {
82: logger->Write(buf);
1.1 root 83: }
1.1.1.9 root 84:
85: // このオブジェクトの ID を文字列にして返す
86: const char *
87: Object::GetIdStr() const
88: {
89: return GetIdStr(GetId());
90: }
91:
92: // 指定のオブジェクト ID を文字列にして返す
93: /*static*/ const char *
1.1.1.10! root 94: Object::GetIdStr(uint target_id)
1.1.1.9 root 95: {
1.1.1.10! root 96: if (target_id < idname.size()) {
1.1.1.9 root 97: return idname[target_id];
98: }
99: return "unknownID?";
100: }
101:
102: // 指定のオブジェクト ID を持つオブジェクトを返す。
103: /*static*/ Object *
1.1.1.10! root 104: Object::GetObject(uint target_id)
1.1.1.9 root 105: {
106: return gMainApp.GetObject(target_id);
107: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.