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),
46 _rotate_size(DefaultFileSize), _file_count(DefaultFileCount),
47 _current_size(0), _current_file(0), _rotation_semaphore(1) {
48 _file_name = make_file_name(name, _pid_str, _vm_start_time_str);
49 }
50
51 void LogFileOutput::set_file_name_parameters(jlong vm_start_time) {
52 int res = jio_snprintf(_pid_str, sizeof(_pid_str), "%d", os::current_process_id());
53 assert(res > 0, "PID buffer too small");
54
55 struct tm local_time;
56 time_t utc_time = vm_start_time / 1000;
57 os::localtime_pd(&utc_time, &local_time);
58 res = (int)strftime(_vm_start_time_str, sizeof(_vm_start_time_str), TimestampFormat, &local_time);
59 assert(res > 0, "VM start time buffer too small.");
60 }
61
62 LogFileOutput::~LogFileOutput() {
63 if (_stream != NULL) {
64 if (fclose(_stream) != 0) {
65 jio_fprintf(defaultStream::error_stream(), "Could not close log file '%s' (%s).\n",
66 _file_name, os::strerror(errno));
67 }
68 }
|
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::Prefix = "file=";
35 const char* LogFileOutput::FileOpenMode = "a";
36 const char* LogFileOutput::PidFilenamePlaceholder = "%p";
37 const char* LogFileOutput::TimestampFilenamePlaceholder = "%t";
38 const char* LogFileOutput::TimestampFormat = "%Y-%m-%d_%H-%M-%S";
39 const char* LogFileOutput::FileSizeOptionKey = "filesize";
40 const char* LogFileOutput::FileCountOptionKey = "filecount";
41 char LogFileOutput::_pid_str[PidBufferSize];
42 char LogFileOutput::_vm_start_time_str[StartTimeBufferSize];
43
44 LogFileOutput::LogFileOutput(const char* name)
45 : LogFileStreamOutput(NULL), _name(os::strdup_check_oom(name, mtLogging)),
46 _file_name(NULL), _archive_name(NULL), _archive_name_len(0),
47 _rotate_size(DefaultFileSize), _file_count(DefaultFileCount),
48 _current_size(0), _current_file(0), _rotation_semaphore(1) {
49 assert(strstr(name, Prefix) == name, "invalid output name '%s': missing prefix: %s", name, Prefix);
50 _file_name = make_file_name(name + strlen(Prefix), _pid_str, _vm_start_time_str);
51 }
52
53 void LogFileOutput::set_file_name_parameters(jlong vm_start_time) {
54 int res = jio_snprintf(_pid_str, sizeof(_pid_str), "%d", os::current_process_id());
55 assert(res > 0, "PID buffer too small");
56
57 struct tm local_time;
58 time_t utc_time = vm_start_time / 1000;
59 os::localtime_pd(&utc_time, &local_time);
60 res = (int)strftime(_vm_start_time_str, sizeof(_vm_start_time_str), TimestampFormat, &local_time);
61 assert(res > 0, "VM start time buffer too small.");
62 }
63
64 LogFileOutput::~LogFileOutput() {
65 if (_stream != NULL) {
66 if (fclose(_stream) != 0) {
67 jio_fprintf(defaultStream::error_stream(), "Could not close log file '%s' (%s).\n",
68 _file_name, os::strerror(errno));
69 }
70 }
|