27 #include "logging/logLevel.hpp"
28 #include "memory/allocation.hpp"
29 #include "utilities/globalDefinitions.hpp"
30
31 class LogOutput;
32
33 // Data structure to keep track of log outputs for a given tagset.
34 // Essentially a sorted linked list going from error level outputs
35 // to outputs of finer levels. Keeps an index from each level to
36 // the first node in the list for the corresponding level.
37 // This allows a log message on, for example, info level to jump
38 // straight into the list where the first info level output can
39 // be found. The log message will then be printed on that output,
40 // as well as all outputs in nodes that follow in the list (which
41 // can be additional info level outputs and/or debug and trace outputs).
42 //
43 // Each instance keeps track of the number of current readers of the list.
44 // To remove a node from the list the node must first be unlinked,
45 // and the memory for that node can be freed whenever the removing
46 // thread observes an active reader count of 0 (after unlinking it).
47 class LogOutputList VALUE_OBJ_CLASS_SPEC {
48 private:
49 struct LogOutputNode : public CHeapObj<mtLogging> {
50 LogOutput* _value;
51 LogOutputNode* _next;
52 LogLevelType _level;
53 };
54
55 LogOutputNode* _level_start[LogLevel::Count];
56 volatile jint _active_readers;
57
58 LogOutputNode* find(const LogOutput* output) const;
59 void remove_output(LogOutputNode* node);
60 void add_output(LogOutput* output, LogLevelType level);
61 void update_output_level(LogOutputNode* node, LogLevelType level);
62
63 // Bookkeeping functions to keep track of number of active readers/iterators for the list.
64 jint increase_readers();
65 jint decrease_readers();
66 void wait_until_no_readers() const;
67
71 _level_start[i] = NULL;
72 }
73 }
74
75 // Test if the outputlist has an output for the given level.
76 bool is_level(LogLevelType level) const {
77 return _level_start[level] != NULL;
78 }
79
80 LogLevelType level_for(const LogOutput* output) const {
81 LogOutputNode* node = this->find(output);
82 if (node == NULL) {
83 return LogLevel::Off;
84 }
85 return node->_level;
86 }
87
88 // Set (add/update/remove) the output to the specified level.
89 void set_output_level(LogOutput* output, LogLevelType level);
90
91 class Iterator VALUE_OBJ_CLASS_SPEC {
92 friend class LogOutputList;
93 private:
94 LogOutputNode* _current;
95 LogOutputList* _list;
96 Iterator(LogOutputList* list, LogOutputNode* start) : _current(start), _list(list) {
97 }
98
99 public:
100 ~Iterator() {
101 _list->decrease_readers();
102 }
103
104 LogOutput* operator*() {
105 return _current->_value;
106 }
107
108 void operator++(int) {
109 _current = _current->_next;
110 }
111
|
27 #include "logging/logLevel.hpp"
28 #include "memory/allocation.hpp"
29 #include "utilities/globalDefinitions.hpp"
30
31 class LogOutput;
32
33 // Data structure to keep track of log outputs for a given tagset.
34 // Essentially a sorted linked list going from error level outputs
35 // to outputs of finer levels. Keeps an index from each level to
36 // the first node in the list for the corresponding level.
37 // This allows a log message on, for example, info level to jump
38 // straight into the list where the first info level output can
39 // be found. The log message will then be printed on that output,
40 // as well as all outputs in nodes that follow in the list (which
41 // can be additional info level outputs and/or debug and trace outputs).
42 //
43 // Each instance keeps track of the number of current readers of the list.
44 // To remove a node from the list the node must first be unlinked,
45 // and the memory for that node can be freed whenever the removing
46 // thread observes an active reader count of 0 (after unlinking it).
47 class LogOutputList {
48 private:
49 struct LogOutputNode : public CHeapObj<mtLogging> {
50 LogOutput* _value;
51 LogOutputNode* _next;
52 LogLevelType _level;
53 };
54
55 LogOutputNode* _level_start[LogLevel::Count];
56 volatile jint _active_readers;
57
58 LogOutputNode* find(const LogOutput* output) const;
59 void remove_output(LogOutputNode* node);
60 void add_output(LogOutput* output, LogLevelType level);
61 void update_output_level(LogOutputNode* node, LogLevelType level);
62
63 // Bookkeeping functions to keep track of number of active readers/iterators for the list.
64 jint increase_readers();
65 jint decrease_readers();
66 void wait_until_no_readers() const;
67
71 _level_start[i] = NULL;
72 }
73 }
74
75 // Test if the outputlist has an output for the given level.
76 bool is_level(LogLevelType level) const {
77 return _level_start[level] != NULL;
78 }
79
80 LogLevelType level_for(const LogOutput* output) const {
81 LogOutputNode* node = this->find(output);
82 if (node == NULL) {
83 return LogLevel::Off;
84 }
85 return node->_level;
86 }
87
88 // Set (add/update/remove) the output to the specified level.
89 void set_output_level(LogOutput* output, LogLevelType level);
90
91 class Iterator {
92 friend class LogOutputList;
93 private:
94 LogOutputNode* _current;
95 LogOutputList* _list;
96 Iterator(LogOutputList* list, LogOutputNode* start) : _current(start), _list(list) {
97 }
98
99 public:
100 ~Iterator() {
101 _list->decrease_readers();
102 }
103
104 LogOutput* operator*() {
105 return _current->_value;
106 }
107
108 void operator++(int) {
109 _current = _current->_next;
110 }
111
|