start.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "start.h"
  2. #include "all.h"
  3. #include <google/protobuf/text_format.h>
  4. #include <util/generic/singleton.h>
  5. #include <util/stream/file.h>
  6. #include <util/stream/output.h>
  7. #include <util/system/env.h>
  8. #include <stdlib.h>
  9. using namespace NLWTrace;
  10. namespace {
  11. struct TTraceManagerHolder {
  12. TManager TraceManager;
  13. TTraceManagerHolder()
  14. : TraceManager(*Singleton<TProbeRegistry>(), true)
  15. {
  16. }
  17. };
  18. void TraceFromEnv(TString path) {
  19. TString script = TUnbufferedFileInput(path).ReadAll();
  20. TQuery query;
  21. bool ok = google::protobuf::TextFormat::ParseFromString(script, &query);
  22. Y_ABORT_UNLESS(ok, "failed to parse protobuf");
  23. Singleton<TTraceManagerHolder>()->TraceManager.New("env", query);
  24. }
  25. } // anonymous namespace
  26. void NLWTrace::StartLwtraceFromEnv() {
  27. static bool started = false;
  28. if (started) {
  29. return;
  30. } else {
  31. started = true;
  32. }
  33. TString path = GetEnv("LWTRACE");
  34. if (!path) {
  35. return;
  36. }
  37. try {
  38. TraceFromEnv(path);
  39. } catch (...) {
  40. Cerr << "failed to load lwtrace script: " << CurrentExceptionMessage() << "\n";
  41. abort();
  42. }
  43. }
  44. void NLWTrace::StartLwtraceFromEnv(std::function<void(TManager&)> prepare) {
  45. TString path = GetEnv("LWTRACE");
  46. if (Y_LIKELY(!path)) {
  47. return;
  48. }
  49. try {
  50. prepare(Singleton<TTraceManagerHolder>()->TraceManager);
  51. TraceFromEnv(path);
  52. } catch (...) {
  53. Cerr << "failed to load lwtrace script: " << CurrentExceptionMessage() << "\n";
  54. abort();
  55. }
  56. }