AdminLog.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include <stdarg.h> // for String_vprintf()
  16. #include "admin/Admin.h"
  17. #include "admin/AdminLog.h"
  18. #include "benc/Dict.h"
  19. #include "benc/List.h"
  20. #include "benc/String.h"
  21. #include "crypto/random/Random.h"
  22. #include "io/Writer.h"
  23. #include "util/log/Log.h"
  24. #include "util/log/Log_impl.h"
  25. #include "util/Hex.h"
  26. #include "util/Identity.h"
  27. #include "util/events/Time.h"
  28. #include "util/events/Timeout.h"
  29. #define MAX_SUBSCRIPTIONS 64
  30. #define FILE_NAME_COUNT 32
  31. struct Subscription
  32. {
  33. /** The log level to match against, all higher levels will also be matched. */
  34. enum Log_Level logLevel;
  35. /** The line number within the file or 0 to match all lines. */
  36. int lineNum;
  37. /** The name of the file to match against or null to match any file. */
  38. const char* file;
  39. /** True if file can be compared with pointer comparison instead of strcmp. */
  40. bool internalFile;
  41. /**
  42. * Dropped messages because they are being sent too fast for UDP interface to handle.
  43. * Reset when the pipes unclog an a message is sent reporting the number of dropped messages.
  44. */
  45. int dropped;
  46. /** The transaction ID of the message which solicited this stream of logs. */
  47. String* txid;
  48. /** A hopefully unique (random) number identifying this stream. */
  49. String* streamId;
  50. /** An allocator which will live during the lifecycle of the Subscription */
  51. struct Allocator* alloc;
  52. };
  53. struct AdminLog
  54. {
  55. struct Log pub;
  56. struct Subscription subscriptions[MAX_SUBSCRIPTIONS];
  57. int subscriptionCount;
  58. /** non-zero if we are logging at this very moment (reentrent logging is not allowed!) */
  59. int logging;
  60. struct Timeout* unpause;
  61. struct Admin* admin;
  62. struct Allocator* alloc;
  63. struct Random* rand;
  64. struct EventBase* base;
  65. Identity
  66. };
  67. static inline bool isMatch(struct Subscription* subscription,
  68. struct AdminLog* logger,
  69. enum Log_Level logLevel,
  70. const char* file,
  71. int line)
  72. {
  73. if (subscription->file) {
  74. if (subscription->file == file) {
  75. // fall through
  76. } else if (!subscription->internalFile && !CString_strcmp(file, subscription->file)) {
  77. // It's the same name but so we'll swap the name for the internal name and then
  78. // it can be compared quickly with a pointer comparison.
  79. subscription->file = file;
  80. subscription->internalFile = true;
  81. } else {
  82. return false;
  83. }
  84. }
  85. if (logLevel < subscription->logLevel) {
  86. return false;
  87. }
  88. if (subscription->lineNum && line != subscription->lineNum) {
  89. return false;
  90. }
  91. return true;
  92. }
  93. static String* STREAM_ID = String_CONST_SO("streamId");
  94. static String* TIME = String_CONST_SO("time");
  95. static String* LEVEL = String_CONST_SO("level");
  96. static String* STR_FILE = String_CONST_SO("file");
  97. static String* LINE = String_CONST_SO("line");
  98. static String* MESSAGE = String_CONST_SO("message");
  99. static Dict* makeLogMessage(struct Subscription* subscription,
  100. struct AdminLog* logger,
  101. enum Log_Level logLevel,
  102. const char* file,
  103. uint32_t line,
  104. String* message,
  105. struct Allocator* alloc)
  106. {
  107. int64_t now = (int64_t) Time_currentTimeSeconds();
  108. Dict* out = Dict_new(alloc);
  109. Dict_putString(out, STREAM_ID, subscription->streamId, alloc);
  110. Dict_putInt(out, TIME, now, alloc);
  111. Dict_putString(out, LEVEL, String_new(Log_nameForLevel(logLevel), alloc), alloc);
  112. Dict_putString(out, STR_FILE, String_new(file, alloc), alloc);
  113. Dict_putInt(out, LINE, line, alloc);
  114. Dict_putString(out, MESSAGE, message, alloc);
  115. return out;
  116. }
  117. static void removeSubscription(struct AdminLog* log, struct Subscription* sub)
  118. {
  119. Allocator_free(sub->alloc);
  120. log->subscriptionCount--;
  121. if (log->subscriptionCount == 0 || sub == &log->subscriptions[log->subscriptionCount]) {
  122. return;
  123. }
  124. Bits_memcpy(sub,
  125. &log->subscriptions[log->subscriptionCount],
  126. sizeof(struct Subscription));
  127. }
  128. static void unpause(void* vAdminLog)
  129. {
  130. struct AdminLog* log = Identity_check((struct AdminLog*) vAdminLog);
  131. // dirty reentrence.
  132. Assert_true(!log->logging);
  133. bool noneDropped = true;
  134. for (int i = log->subscriptionCount - 1; i >= 0; i--) {
  135. int dropped = log->subscriptions[i].dropped;
  136. if (!dropped) { continue; }
  137. noneDropped = false;
  138. log->subscriptions[i].dropped = 0;
  139. Log_warn((struct Log*) log,
  140. "UDPInterface cannot handle the logging, [%d] messages dropped", dropped);
  141. if (log->subscriptions[i].dropped) {
  142. // oh well, we'll try again later.
  143. log->subscriptions[i].dropped += dropped;
  144. }
  145. }
  146. if (noneDropped && false) {
  147. Timeout_clearTimeout(log->unpause);
  148. log->unpause = NULL;
  149. }
  150. }
  151. static void doLog(struct Log* genericLog,
  152. enum Log_Level logLevel,
  153. const char* fullFilePath,
  154. int line,
  155. const char* format,
  156. va_list args)
  157. {
  158. struct AdminLog* log = Identity_check((struct AdminLog*) genericLog);
  159. String* message = NULL;
  160. struct Allocator* logLineAlloc = NULL;
  161. if (log->logging) { return; }
  162. log->logging++;
  163. for (int i = log->subscriptionCount - 1; i >= 0; i--) {
  164. if (!isMatch(&log->subscriptions[i], log, logLevel, fullFilePath, line)) { continue; }
  165. if (log->subscriptions[i].dropped) {
  166. log->subscriptions[i].dropped++;
  167. continue;
  168. }
  169. if (!message) {
  170. logLineAlloc = Allocator_child(log->alloc);
  171. message = String_vprintf(logLineAlloc, format, args);
  172. // Strip all of the annoying \n marks in the log entries.
  173. if (message->len > 0 && message->bytes[message->len - 1] == '\n') {
  174. message->len--;
  175. }
  176. }
  177. Dict* d = makeLogMessage(&log->subscriptions[i],
  178. log,
  179. logLevel,
  180. fullFilePath,
  181. line,
  182. message,
  183. logLineAlloc);
  184. int ret = Admin_sendMessage(d, log->subscriptions[i].txid, log->admin);
  185. if (ret == Admin_sendMessage_CHANNEL_CLOSED) {
  186. removeSubscription(log, &log->subscriptions[i]);
  187. } else if (ret) {
  188. log->subscriptions[i].dropped++;
  189. if (!log->unpause) {
  190. log->unpause = Timeout_setInterval(unpause, log, 10, log->base, log->alloc);
  191. }
  192. }
  193. }
  194. if (logLineAlloc) {
  195. Allocator_free(logLineAlloc);
  196. }
  197. Assert_true(!--log->logging);
  198. }
  199. static void subscribe(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  200. {
  201. struct AdminLog* log = Identity_check((struct AdminLog*) vcontext);
  202. String* levelName = Dict_getStringC(args, "level");
  203. enum Log_Level level = (levelName) ? Log_levelForName(levelName->bytes) : Log_Level_DEBUG;
  204. int64_t* lineNumPtr = Dict_getIntC(args, "line");
  205. String* fileStr = Dict_getStringC(args, "file");
  206. if (fileStr && !fileStr->len) { fileStr = NULL; }
  207. char* error = "2+2=5";
  208. if (level == Log_Level_INVALID) {
  209. level = Log_Level_KEYS;
  210. }
  211. if (lineNumPtr && *lineNumPtr < 0) {
  212. error = "Invalid line number, must be positive or 0 to signify any line is acceptable.";
  213. } else if (log->subscriptionCount >= MAX_SUBSCRIPTIONS) {
  214. error = "Max subscription count reached.";
  215. } else {
  216. struct Subscription* sub = &log->subscriptions[log->subscriptionCount];
  217. Bits_memset(sub, 0, sizeof(struct Subscription));
  218. sub->logLevel = level;
  219. sub->alloc = Allocator_child(log->alloc);
  220. String* fileStrCpy = String_clone(fileStr, sub->alloc);
  221. sub->file = (fileStrCpy) ? fileStrCpy->bytes : NULL;
  222. sub->lineNum = (lineNumPtr) ? *lineNumPtr : 0;
  223. sub->txid = String_clone(txid, sub->alloc);
  224. uint8_t streamId[8];
  225. Random_bytes(log->rand, streamId, 8);
  226. uint8_t streamIdHex[20];
  227. Hex_encode(streamIdHex, 20, streamId, 8);
  228. sub->streamId = String_new(streamIdHex, sub->alloc);
  229. Dict response = Dict_CONST(
  230. String_CONST("error"), String_OBJ(String_CONST("none")), Dict_CONST(
  231. String_CONST("streamId"), String_OBJ(sub->streamId), NULL
  232. ));
  233. Admin_sendMessage(&response, txid, log->admin);
  234. log->subscriptionCount++;
  235. return;
  236. }
  237. Dict response = Dict_CONST(
  238. String_CONST("error"), String_OBJ(String_CONST(error)), NULL
  239. );
  240. Admin_sendMessage(&response, txid, log->admin);
  241. }
  242. static void unsubscribe(Dict* args, void* vcontext, String* txid, struct Allocator* requestAlloc)
  243. {
  244. struct AdminLog* log = Identity_check((struct AdminLog*) vcontext);
  245. String* streamIdHex = Dict_getStringC(args, "streamId");
  246. uint8_t streamId[8];
  247. char* error = NULL;
  248. if (streamIdHex->len != 16 || Hex_decode(streamId, 8, (uint8_t*)streamIdHex->bytes, 16) != 8) {
  249. error = "Invalid streamId.";
  250. } else {
  251. error = "No such subscription.";
  252. for (int i = 0; i < (int)log->subscriptionCount; i++) {
  253. if (String_equals(streamIdHex, log->subscriptions[i].streamId)) {
  254. removeSubscription(log, &log->subscriptions[i]);
  255. error = "none";
  256. break;
  257. }
  258. }
  259. }
  260. Dict response = Dict_CONST(
  261. String_CONST("error"), String_OBJ(String_CONST(error)), NULL
  262. );
  263. Admin_sendMessage(&response, txid, log->admin);
  264. }
  265. static void logMany(Dict* args, void* vcontext, String* txid, struct Allocator* alloc)
  266. {
  267. struct AdminLog* log = Identity_check((struct AdminLog*) vcontext);
  268. int64_t* countPtr = Dict_getIntC(args, "count");
  269. uint32_t count = *countPtr;
  270. for (uint32_t i = 0; i < count; i++) {
  271. Log_debug((struct Log*)log, "This is message number [%d] of total [%d]", i, count);
  272. }
  273. Dict response = Dict_CONST(
  274. String_CONST("error"), String_OBJ(String_CONST("none")), NULL
  275. );
  276. Admin_sendMessage(&response, txid, log->admin);
  277. }
  278. static void subscriptions(Dict* args, void* vcontext, String* txid, struct Allocator* alloc)
  279. {
  280. struct AdminLog* log = Identity_check((struct AdminLog*) vcontext);
  281. Dict* msg = Dict_new(alloc);
  282. List* entries = List_new(alloc);
  283. Dict_putListC(msg, "entries", entries, alloc);
  284. for (int i = 0; i < (int)log->subscriptionCount; i++) {
  285. Dict* entry = Dict_new(alloc);
  286. struct Subscription* sub = &log->subscriptions[i];
  287. Dict_putString(entry, LEVEL, String_new(Log_nameForLevel(sub->logLevel), alloc), alloc);
  288. if (sub->file) {
  289. Dict_putString(entry, STR_FILE, String_new(sub->file, alloc), alloc);
  290. }
  291. Dict_putInt(entry, LINE, sub->lineNum, alloc);
  292. Dict_putIntC(entry, "dropped", sub->dropped, alloc);
  293. Dict_putIntC(entry, "internalFile", sub->internalFile, alloc);
  294. Dict_putStringC(entry, "streamId", sub->streamId, alloc);
  295. List_addDict(entries, entry, alloc);
  296. }
  297. Admin_sendMessage(msg, txid, log->admin);
  298. }
  299. struct Log* AdminLog_registerNew(struct Admin* admin,
  300. struct Allocator* alloc,
  301. struct Random* rand,
  302. struct EventBase* base)
  303. {
  304. struct AdminLog* log = Allocator_clone(alloc, (&(struct AdminLog) {
  305. .pub = {
  306. .print = doLog
  307. },
  308. .admin = admin,
  309. .alloc = alloc,
  310. .rand = rand,
  311. .base = base
  312. }));
  313. Identity_set(log);
  314. Admin_registerFunction("AdminLog_subscribe", subscribe, log, true,
  315. ((struct Admin_FunctionArg[]) {
  316. { .name = "level", .required = 0, .type = "String" },
  317. { .name = "line", .required = 0, .type = "Int" },
  318. { .name = "file", .required = 0, .type = "String" }
  319. }), admin);
  320. Admin_registerFunction("AdminLog_unsubscribe", unsubscribe, log, true,
  321. ((struct Admin_FunctionArg[]) {
  322. { .name = "streamId", .required = 1, .type = "String" }
  323. }), admin);
  324. Admin_registerFunction("AdminLog_subscriptions", subscriptions, log, true, NULL, admin);
  325. Admin_registerFunction("AdminLog_logMany", logMany, log, true,
  326. ((struct Admin_FunctionArg[]) {
  327. { .name = "count", .required = 1, .type = "Int" }
  328. }), admin);
  329. return &log->pub;
  330. }