1
0

AdminLog.c 13 KB

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