lwtrace_example3.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include <library/cpp/lwtrace/all.h>
  2. #include <google/protobuf/text_format.h>
  3. #include "my_action.h"
  4. #define LWTRACE_EXAMPLE_PROVIDER(PROBE, EVENT, GROUPS, TYPES, NAMES) \
  5. PROBE(IterationProbe, GROUPS(), TYPES(i32, double), NAMES("n", "result")) \
  6. /**/
  7. LWTRACE_DECLARE_PROVIDER(LWTRACE_EXAMPLE_PROVIDER)
  8. LWTRACE_DEFINE_PROVIDER(LWTRACE_EXAMPLE_PROVIDER)
  9. void InitLWTrace() {
  10. NLWTrace::StartLwtraceFromEnv([=](NLWTrace::TManager& mngr) {
  11. mngr.RegisterCustomAction<TMyActionExecutor>();
  12. });
  13. }
  14. long double Fact(int n) {
  15. if (n < 0) {
  16. ythrow yexception() << "N! is undefined for negative N (" << n << ")";
  17. }
  18. double result = 1;
  19. for (; n > 1; --n) {
  20. GLOBAL_LWPROBE(LWTRACE_EXAMPLE_PROVIDER, IterationProbe, n, result);
  21. result *= n;
  22. }
  23. return result;
  24. }
  25. void FactorialCalculator() {
  26. i32 n;
  27. Cout << "Enter a number: ";
  28. TString str;
  29. Cin >> n;
  30. double factN = Fact(n);
  31. Cout << n << "! = " << factN << Endl << Endl;
  32. }
  33. int main() {
  34. InitLWTrace();
  35. FactorialCalculator();
  36. return 0;
  37. }