0N/A/*
2362N/A * Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#include "util.h"
0N/A
0N/A#include <time.h>
0N/A#include <errno.h>
0N/A#include <sys/types.h>
0N/A
0N/A#include "proc_md.h"
0N/A
0N/A#include "log_messages.h"
0N/A
0N/A#ifdef JDWP_LOGGING
0N/A
0N/A#define MAXLEN_INTEGER 20
0N/A#define MAXLEN_FILENAME 256
0N/A#define MAXLEN_TIMESTAMP 80
0N/A#define MAXLEN_LOCATION (MAXLEN_FILENAME+MAXLEN_INTEGER+16)
0N/A#define MAXLEN_MESSAGE 256
0N/A#define MAXLEN_EXEC (MAXLEN_FILENAME*2+MAXLEN_INTEGER+16)
0N/A
0N/Astatic MUTEX_T my_mutex = MUTEX_INIT;
0N/A
0N/A/* Static variables (should be protected with mutex) */
0N/Astatic int logging;
0N/Astatic FILE * log_file;
0N/Astatic char logging_filename[MAXLEN_FILENAME+1+6];
0N/Astatic char location_stamp[MAXLEN_LOCATION+1];
0N/Astatic PID_T processPid;
0N/Astatic int open_count;
0N/A
0N/A/* Ascii id of current native thread. */
0N/Astatic void
0N/Aget_time_stamp(char *tbuf, size_t ltbuf)
0N/A{
0N/A char format[MAXLEN_TIMESTAMP+1];
0N/A unsigned millisecs = 0;
0N/A time_t t = 0;
0N/A
0N/A GETMILLSECS(millisecs);
0N/A if ( time(&t) == (time_t)(-1) )
0N/A t = 0;
0N/A (void)strftime(format, sizeof(format),
0N/A /* Break this string up for SCCS's sake */
0N/A "%" "d.%" "m.%" "Y %" "T.%%.3d %" "Z", localtime(&t));
0N/A (void)snprintf(tbuf, ltbuf, format, (int)(millisecs));
0N/A}
0N/A
0N/A/* Get basename of filename */
0N/Astatic const char *
0N/Afile_basename(const char *file)
0N/A{
0N/A char *p1;
0N/A char *p2;
0N/A
0N/A if ( file==NULL )
0N/A return "unknown";
0N/A p1 = strrchr(file, '\\');
0N/A p2 = strrchr(file, '/');
0N/A p1 = ((p1 > p2) ? p1 : p2);
0N/A if (p1 != NULL) {
0N/A file = p1 + 1;
0N/A }
0N/A return file;
0N/A}
0N/A
0N/A/* Fill in the exact source location of the LOG entry. */
0N/Astatic void
0N/Afill_location_stamp(const char *flavor, const char *file, int line)
0N/A{
0N/A (void)snprintf(location_stamp, sizeof(location_stamp),
0N/A "%s:\"%s\":%d;",
0N/A flavor, file_basename(file), line);
0N/A location_stamp[sizeof(location_stamp)-1] = 0;
0N/A}
0N/A
0N/A/* Begin a log entry. */
0N/Avoid
0N/Alog_message_begin(const char *flavor, const char *file, int line)
0N/A{
0N/A MUTEX_LOCK(my_mutex); /* Unlocked in log_message_end() */
0N/A if ( logging ) {
0N/A location_stamp[0] = 0;
0N/A fill_location_stamp(flavor, file, line);
0N/A }
0N/A}
0N/A
0N/A/* Standard Logging Format Entry */
0N/Astatic void
0N/Astandard_logging_format(FILE *fp,
0N/A const char *datetime,
0N/A const char *level,
0N/A const char *product,
0N/A const char *module,
0N/A const char *optional,
0N/A const char *messageID,
0N/A const char *message)
0N/A{
0N/A const char *format;
0N/A
0N/A /* "[#|Date&Time&Zone|LogLevel|ProductName|ModuleID|
0N/A * OptionalKey1=Value1;OptionalKeyN=ValueN|MessageID:MessageText|#]\n"
0N/A */
0N/A
0N/A format="[#|%s|%s|%s|%s|%s|%s:%s|#]\n";
0N/A
0N/A print_message(fp, "", "", format,
0N/A datetime,
0N/A level,
0N/A product,
0N/A module,
0N/A optional,
0N/A messageID,
0N/A message);
0N/A}
0N/A
0N/A/* End a log entry */
0N/Avoid
0N/Alog_message_end(const char *format, ...)
0N/A{
0N/A if ( logging ) {
0N/A va_list ap;
0N/A THREAD_T tid;
0N/A char datetime[MAXLEN_TIMESTAMP+1];
0N/A const char *level;
0N/A const char *product;
0N/A const char *module;
0N/A char optional[MAXLEN_INTEGER+6+MAXLEN_INTEGER+6+MAXLEN_LOCATION+1];
0N/A const char *messageID;
0N/A char message[MAXLEN_MESSAGE+1];
0N/A
0N/A /* Grab the location, start file if needed, and clear the lock */
0N/A if ( log_file == NULL && open_count == 0 && logging_filename[0] != 0 ) {
0N/A open_count++;
0N/A log_file = fopen(logging_filename, "w");
0N/A if ( log_file!=NULL ) {
0N/A (void)setvbuf(log_file, NULL, _IOLBF, BUFSIZ);
0N/A } else {
0N/A logging = 0;
0N/A }
0N/A }
0N/A
0N/A if ( log_file != NULL ) {
0N/A
0N/A /* Get the rest of the needed information */
0N/A tid = GET_THREAD_ID();
0N/A level = "FINEST"; /* FIXUP? */
0N/A product = "J2SE1.5"; /* FIXUP? */
0N/A module = "jdwp"; /* FIXUP? */
0N/A messageID = ""; /* FIXUP: Unique message string ID? */
0N/A (void)snprintf(optional, sizeof(optional),
0N/A "LOC=%s;PID=%d;THR=t@%d",
0N/A location_stamp,
0N/A (int)processPid,
0N/A (int)tid);
0N/A
0N/A /* Construct message string. */
0N/A va_start(ap, format);
0N/A (void)vsnprintf(message, sizeof(message), format, ap);
0N/A va_end(ap);
0N/A
0N/A get_time_stamp(datetime, sizeof(datetime));
0N/A
0N/A /* Send out standard logging format message */
0N/A standard_logging_format(log_file,
0N/A datetime,
0N/A level,
0N/A product,
0N/A module,
0N/A optional,
0N/A messageID,
0N/A message);
0N/A }
0N/A location_stamp[0] = 0;
0N/A }
0N/A MUTEX_UNLOCK(my_mutex); /* Locked in log_message_begin() */
0N/A}
0N/A
0N/A#endif
0N/A
0N/A/* Set up the logging with the name of a logging file. */
0N/Avoid
0N/Asetup_logging(const char *filename, unsigned flags)
0N/A{
0N/A#ifdef JDWP_LOGGING
0N/A FILE *fp = NULL;
0N/A
0N/A /* Turn off logging */
0N/A logging = 0;
0N/A gdata->log_flags = 0;
0N/A
0N/A /* Just return if not doing logging */
0N/A if ( filename==NULL || flags==0 )
0N/A return;
0N/A
0N/A /* Create potential filename for logging */
0N/A processPid = GETPID();
0N/A (void)snprintf(logging_filename, sizeof(logging_filename),
0N/A "%s.%d", filename, (int)processPid);
0N/A
0N/A /* Turn on logging (do this last) */
0N/A logging = 1;
0N/A gdata->log_flags = flags;
0N/A
0N/A#endif
0N/A}
0N/A
0N/A/* Finish up logging, flush output to the logfile. */
0N/Avoid
0N/Afinish_logging(int exit_code)
0N/A{
0N/A#ifdef JDWP_LOGGING
0N/A MUTEX_LOCK(my_mutex);
0N/A if ( logging ) {
0N/A logging = 0;
0N/A if ( log_file != NULL ) {
0N/A (void)fflush(log_file);
0N/A (void)fclose(log_file);
0N/A log_file = NULL;
0N/A }
0N/A }
0N/A MUTEX_UNLOCK(my_mutex);
0N/A#endif
0N/A}