log_settings.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #include "log_settings.h"
  2. #include <util/stream/str.h>
  3. namespace NActors {
  4. namespace NLog {
  5. TSettings::TSettings(const TActorId& loggerActorId, const EComponent loggerComponent,
  6. EComponent minVal, EComponent maxVal, EComponentToStringFunc func,
  7. EPriority defPriority, EPriority defSamplingPriority,
  8. ui32 defSamplingRate, ui64 timeThresholdMs)
  9. : LoggerActorId(loggerActorId)
  10. , LoggerComponent(loggerComponent)
  11. , TimeThresholdMs(timeThresholdMs)
  12. , AllowDrop(true)
  13. , ThrottleDelay(TDuration::MilliSeconds(100))
  14. , MinVal(0)
  15. , MaxVal(0)
  16. , Mask(0)
  17. , DefPriority(defPriority)
  18. , DefSamplingPriority(defSamplingPriority)
  19. , DefSamplingRate(defSamplingRate)
  20. , UseLocalTimestamps(false)
  21. , Format(PLAIN_FULL_FORMAT)
  22. , ShortHostName("")
  23. , ClusterName("")
  24. {
  25. Append(minVal, maxVal, func);
  26. }
  27. TSettings::TSettings(const TActorId& loggerActorId, const EComponent loggerComponent,
  28. EPriority defPriority, EPriority defSamplingPriority,
  29. ui32 defSamplingRate, ui64 timeThresholdMs)
  30. : LoggerActorId(loggerActorId)
  31. , LoggerComponent(loggerComponent)
  32. , TimeThresholdMs(timeThresholdMs)
  33. , AllowDrop(true)
  34. , ThrottleDelay(TDuration::MilliSeconds(100))
  35. , MinVal(0)
  36. , MaxVal(0)
  37. , Mask(0)
  38. , DefPriority(defPriority)
  39. , DefSamplingPriority(defSamplingPriority)
  40. , DefSamplingRate(defSamplingRate)
  41. , UseLocalTimestamps(false)
  42. , Format(PLAIN_FULL_FORMAT)
  43. , ShortHostName("")
  44. , ClusterName("")
  45. {
  46. }
  47. void TSettings::Append(EComponent minVal, EComponent maxVal, EComponentToStringFunc func) {
  48. Y_VERIFY(minVal >= 0, "NLog::TSettings: minVal must be non-negative");
  49. Y_VERIFY(maxVal > minVal, "NLog::TSettings: maxVal must be greater than minVal");
  50. // update bounds
  51. if (!MaxVal || minVal < MinVal) {
  52. MinVal = minVal;
  53. }
  54. if (!MaxVal || maxVal > MaxVal) {
  55. MaxVal = maxVal;
  56. // expand ComponentNames to the new bounds
  57. auto oldMask = Mask;
  58. Mask = PowerOf2Mask(MaxVal);
  59. TArrayHolder<TAtomic> oldComponentInfo(new TAtomic[Mask + 1]);
  60. ComponentInfo.Swap(oldComponentInfo);
  61. int startVal = oldMask ? oldMask + 1 : 0;
  62. for (int i = 0; i < startVal; i++) {
  63. AtomicSet(ComponentInfo[i], AtomicGet(oldComponentInfo[i]));
  64. }
  65. TComponentSettings defSetting(DefPriority, DefSamplingPriority, DefSamplingRate);
  66. for (int i = startVal; i < Mask + 1; i++) {
  67. AtomicSet(ComponentInfo[i], defSetting.Raw.Data);
  68. }
  69. ComponentNames.resize(Mask + 1);
  70. }
  71. // assign new names but validate if newly added members were not used before
  72. for (int i = minVal; i <= maxVal; i++) {
  73. Y_VERIFY(!ComponentNames[i], "component name at %d already set: %s",
  74. i, ComponentNames[i].data());
  75. ComponentNames[i] = func(i);
  76. }
  77. }
  78. int TSettings::SetLevelImpl(
  79. const TString& name, bool isSampling,
  80. EPriority priority, EComponent component, TString& explanation) {
  81. TString titleName(name);
  82. titleName.to_title();
  83. // check priority
  84. if (!IsValidPriority(priority)) {
  85. TStringStream str;
  86. str << "Invalid " << name;
  87. explanation = str.Str();
  88. return 1;
  89. }
  90. if (component == InvalidComponent) {
  91. for (int i = 0; i < Mask + 1; i++) {
  92. TComponentSettings settings = AtomicGet(ComponentInfo[i]);
  93. if (isSampling) {
  94. settings.Raw.X.SamplingLevel = priority;
  95. } else {
  96. settings.Raw.X.Level = priority;
  97. }
  98. AtomicSet(ComponentInfo[i], settings.Raw.Data);
  99. }
  100. TStringStream str;
  101. str << titleName
  102. << " for all components has been changed to "
  103. << PriorityToString(EPrio(priority));
  104. explanation = str.Str();
  105. return 0;
  106. } else {
  107. if (!IsValidComponent(component)) {
  108. explanation = "Invalid component";
  109. return 1;
  110. }
  111. TComponentSettings settings = AtomicGet(ComponentInfo[component]);
  112. EPriority oldPriority;
  113. if (isSampling) {
  114. oldPriority = (EPriority)settings.Raw.X.SamplingLevel;
  115. settings.Raw.X.SamplingLevel = priority;
  116. } else {
  117. oldPriority = (EPriority)settings.Raw.X.Level;
  118. settings.Raw.X.Level = priority;
  119. }
  120. AtomicSet(ComponentInfo[component], settings.Raw.Data);
  121. TStringStream str;
  122. str << titleName << " for the component " << ComponentNames[component]
  123. << " has been changed from " << PriorityToString(EPrio(oldPriority))
  124. << " to " << PriorityToString(EPrio(priority));
  125. explanation = str.Str();
  126. return 0;
  127. }
  128. }
  129. int TSettings::SetLevel(EPriority priority, EComponent component, TString& explanation) {
  130. return SetLevelImpl("priority", false,
  131. priority, component, explanation);
  132. }
  133. int TSettings::SetSamplingLevel(EPriority priority, EComponent component, TString& explanation) {
  134. return SetLevelImpl("sampling priority", true,
  135. priority, component, explanation);
  136. }
  137. int TSettings::SetSamplingRate(ui32 sampling, EComponent component, TString& explanation) {
  138. if (component == InvalidComponent) {
  139. for (int i = 0; i < Mask + 1; i++) {
  140. TComponentSettings settings = AtomicGet(ComponentInfo[i]);
  141. settings.Raw.X.SamplingRate = sampling;
  142. AtomicSet(ComponentInfo[i], settings.Raw.Data);
  143. }
  144. TStringStream str;
  145. str << "Sampling rate for all components has been changed to " << sampling;
  146. explanation = str.Str();
  147. } else {
  148. if (!IsValidComponent(component)) {
  149. explanation = "Invalid component";
  150. return 1;
  151. }
  152. TComponentSettings settings = AtomicGet(ComponentInfo[component]);
  153. ui32 oldSampling = settings.Raw.X.SamplingRate;
  154. settings.Raw.X.SamplingRate = sampling;
  155. AtomicSet(ComponentInfo[component], settings.Raw.Data);
  156. TStringStream str;
  157. str << "Sampling rate for the component " << ComponentNames[component]
  158. << " has been changed from " << oldSampling
  159. << " to " << sampling;
  160. explanation = str.Str();
  161. }
  162. return 0;
  163. }
  164. int TSettings::PowerOf2Mask(int val) {
  165. int mask = 1;
  166. while ((val & mask) != val) {
  167. mask <<= 1;
  168. mask |= 1;
  169. }
  170. return mask;
  171. }
  172. bool TSettings::IsValidPriority(EPriority priority) {
  173. return priority == PRI_EMERG || priority == PRI_ALERT ||
  174. priority == PRI_CRIT || priority == PRI_ERROR ||
  175. priority == PRI_WARN || priority == PRI_NOTICE ||
  176. priority == PRI_INFO || priority == PRI_DEBUG || priority == PRI_TRACE;
  177. }
  178. bool TSettings::IsValidComponent(EComponent component) {
  179. return (MinVal <= component) && (component <= MaxVal) && !ComponentNames[component].empty();
  180. }
  181. void TSettings::SetAllowDrop(bool val) {
  182. AllowDrop = val;
  183. }
  184. void TSettings::SetThrottleDelay(TDuration value) {
  185. ThrottleDelay = value;
  186. }
  187. void TSettings::SetUseLocalTimestamps(bool value) {
  188. UseLocalTimestamps = value;
  189. }
  190. EComponent TSettings::FindComponent(const TStringBuf& componentName) const {
  191. if (componentName.empty())
  192. return InvalidComponent;
  193. for (EComponent component = MinVal; component <= MaxVal; ++component) {
  194. if (ComponentNames[component] == componentName)
  195. return component;
  196. }
  197. return InvalidComponent;
  198. }
  199. }
  200. }