my_action.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #pragma once
  2. #include <library/cpp/lwtrace/all.h>
  3. #include <util/stream/file.h>
  4. // Example of custom state for custom executors
  5. // Holds file for output from TMyActionExecutor
  6. class TMyFile: public NLWTrace::IResource {
  7. private:
  8. TMutex Mutex;
  9. THolder<TUnbufferedFileOutput> File;
  10. public:
  11. // Note that this class must have default ctor (it's declared here just for clearness)
  12. TMyFile() {
  13. }
  14. // Note that dtor will be called by TManager::Delete() after detachment and destruction of all executors
  15. ~TMyFile() {
  16. }
  17. // Some kind of state initialization
  18. // Can be called only from executor constructor, and should not be thread-safe
  19. void Open(const TString& path) {
  20. if (File) {
  21. // We must avoid double open because each executor will call Open() on the same object
  22. // if the same file was specified in Opts
  23. return;
  24. }
  25. File.Reset(new TUnbufferedFileOutput(path));
  26. }
  27. // Outputs a line to opened file
  28. // Can be called from DoExecute() and must be thread-safe
  29. void Output(const TString& line) {
  30. Y_ABORT_UNLESS(File);
  31. TGuard<TMutex> g(Mutex); // Because DoExecute() call can come from any thread
  32. *File << line << Endl;
  33. }
  34. };
  35. // Action that prints events to specified file
  36. class TMyActionExecutor: public NLWTrace::TCustomActionExecutor {
  37. private:
  38. TMyFile& File;
  39. public:
  40. TMyActionExecutor(NLWTrace::TProbe* probe, const NLWTrace::TCustomAction& action, NLWTrace::TSession* session)
  41. : NLWTrace::TCustomActionExecutor(probe, false /* not destructive */)
  42. , File(session->Resources().Get<TMyFile>("FileHolder/" + action.GetOpts(0))) // unique state id must include your d
  43. {
  44. if (action.GetOpts().size() != 1) {
  45. yexception() << "wrong number of Opts in MyAction";
  46. }
  47. File.Open(action.GetOpts(0));
  48. }
  49. static const char* GetActionName() {
  50. static const char name[] = "MyAction";
  51. return name;
  52. }
  53. private:
  54. virtual bool DoExecute(NLWTrace::TOrbit&, const NLWTrace::TParams& params) {
  55. // Serialize param values to strings
  56. TString paramValues[LWTRACE_MAX_PARAMS];
  57. Probe->Event.Signature.SerializeParams(params, paramValues);
  58. // Generate output line
  59. TStringStream ss;
  60. ss << "TMyActionExecutor>>> ";
  61. ss << Probe->Event.Name;
  62. for (ui32 i = 0; i < Probe->Event.Signature.ParamCount; ++i) {
  63. ss << " " << Probe->Event.Signature.ParamNames[i] << "=" << paramValues[i];
  64. }
  65. // Write line to file
  66. File.Output(ss.Str());
  67. // Executors can chain if you specify multiple actions in one block (in trace query).
  68. // So return true to continue execution of actions for given trace query block (on current triggered event)
  69. // or return false if this action is the last for this block
  70. return true;
  71. }
  72. };