9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24 #include "precompiled.hpp"
25 #include "logging/log.hpp"
26 #include "logging/logConfiguration.hpp"
27 #include "logging/logFileOutput.hpp"
28 #include "memory/allocation.inline.hpp"
29 #include "runtime/os.inline.hpp"
30 #include "utilities/globalDefinitions.hpp"
31 #include "utilities/defaultStream.hpp"
32
33 const char* LogFileOutput::FileOpenMode = "a";
34 const char* LogFileOutput::PidFilenamePlaceholder = "%p";
35 const char* LogFileOutput::TimestampFilenamePlaceholder = "%t";
36 const char* LogFileOutput::TimestampFormat = "%Y-%m-%d_%H-%M-%S";
37 const char* LogFileOutput::FileSizeOptionKey = "filesize";
38 const char* LogFileOutput::FileCountOptionKey = "filecount";
39 char LogFileOutput::_pid_str[PidBufferSize];
40 char LogFileOutput::_vm_start_time_str[StartTimeBufferSize];
41
42 LogFileOutput::LogFileOutput(const char* name)
43 : LogFileStreamOutput(NULL), _name(os::strdup_check_oom(name, mtLogging)),
44 _file_name(NULL), _archive_name(NULL), _archive_name_len(0), _current_size(0),
45 _rotate_size(0), _current_file(1), _file_count(0), _rotation_semaphore(1) {
46 _file_name = make_file_name(name, _pid_str, _vm_start_time_str);
47 }
48
99 char* equals_pos = strchr(pos, '=');
100 if (equals_pos == NULL) {
101 success = false;
102 break;
103 }
104 char* key = pos;
105 char* value_str = equals_pos + 1;
106 *equals_pos = '\0';
107
108 if (strcmp(FileCountOptionKey, key) == 0) {
109 size_t value = parse_value(value_str);
110 if (value == SIZE_MAX || value >= UINT_MAX) {
111 success = false;
112 break;
113 }
114 _file_count = static_cast<uint>(value);
115 _file_count_max_digits = static_cast<uint>(log10(static_cast<double>(_file_count)) + 1);
116 _archive_name_len = 2 + strlen(_file_name) + _file_count_max_digits;
117 _archive_name = NEW_C_HEAP_ARRAY(char, _archive_name_len, mtLogging);
118 } else if (strcmp(FileSizeOptionKey, key) == 0) {
119 size_t value = parse_value(value_str);
120 if (value == SIZE_MAX || value > SIZE_MAX / K) {
121 success = false;
122 break;
123 }
124 _rotate_size = value * K;
125 } else {
126 success = false;
127 break;
128 }
129 pos = comma_pos + 1;
130 } while (comma_pos != NULL);
131
132 os::free(opts);
133 return success;
134 }
135
136 bool LogFileOutput::initialize(const char* options) {
137 if (!configure_rotation(options)) {
138 return false;
139 }
140 _stream = fopen(_file_name, FileOpenMode);
141 if (_stream == NULL) {
142 log_error(logging)("Could not open log file '%s' (%s).\n", _file_name, os::strerror(errno));
143 return false;
144 }
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24 #include "precompiled.hpp"
25 #include "logging/log.hpp"
26 #include "logging/logConfiguration.hpp"
27 #include "logging/logFileOutput.hpp"
28 #include "memory/allocation.inline.hpp"
29 #include "runtime/arguments.hpp"
30 #include "runtime/os.inline.hpp"
31 #include "utilities/globalDefinitions.hpp"
32 #include "utilities/defaultStream.hpp"
33
34 const char* LogFileOutput::FileOpenMode = "a";
35 const char* LogFileOutput::PidFilenamePlaceholder = "%p";
36 const char* LogFileOutput::TimestampFilenamePlaceholder = "%t";
37 const char* LogFileOutput::TimestampFormat = "%Y-%m-%d_%H-%M-%S";
38 const char* LogFileOutput::FileSizeOptionKey = "filesize";
39 const char* LogFileOutput::FileCountOptionKey = "filecount";
40 char LogFileOutput::_pid_str[PidBufferSize];
41 char LogFileOutput::_vm_start_time_str[StartTimeBufferSize];
42
43 LogFileOutput::LogFileOutput(const char* name)
44 : LogFileStreamOutput(NULL), _name(os::strdup_check_oom(name, mtLogging)),
45 _file_name(NULL), _archive_name(NULL), _archive_name_len(0), _current_size(0),
46 _rotate_size(0), _current_file(1), _file_count(0), _rotation_semaphore(1) {
47 _file_name = make_file_name(name, _pid_str, _vm_start_time_str);
48 }
49
100 char* equals_pos = strchr(pos, '=');
101 if (equals_pos == NULL) {
102 success = false;
103 break;
104 }
105 char* key = pos;
106 char* value_str = equals_pos + 1;
107 *equals_pos = '\0';
108
109 if (strcmp(FileCountOptionKey, key) == 0) {
110 size_t value = parse_value(value_str);
111 if (value == SIZE_MAX || value >= UINT_MAX) {
112 success = false;
113 break;
114 }
115 _file_count = static_cast<uint>(value);
116 _file_count_max_digits = static_cast<uint>(log10(static_cast<double>(_file_count)) + 1);
117 _archive_name_len = 2 + strlen(_file_name) + _file_count_max_digits;
118 _archive_name = NEW_C_HEAP_ARRAY(char, _archive_name_len, mtLogging);
119 } else if (strcmp(FileSizeOptionKey, key) == 0) {
120 success = Arguments::atomull(value_str, &_rotate_size);
121 } else {
122 success = false;
123 break;
124 }
125 pos = comma_pos + 1;
126 } while (comma_pos != NULL);
127
128 os::free(opts);
129 return success;
130 }
131
132 bool LogFileOutput::initialize(const char* options) {
133 if (!configure_rotation(options)) {
134 return false;
135 }
136 _stream = fopen(_file_name, FileOpenMode);
137 if (_stream == NULL) {
138 log_error(logging)("Could not open log file '%s' (%s).\n", _file_name, os::strerror(errno));
139 return false;
140 }
|