src/java.logging/share/classes/java/util/logging/LogRecord.java
author ihse
Mon, 19 Feb 2018 23:25:11 +0100
branchihse-cflags-rewrite-branch
changeset 56155 f98e22c513dd
parent 47216 71c04702a3d5
permissions -rw-r--r--
Make configure compile.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
43197
883675f526b8 8162577: Standardize logging levels
dfuchs
parents: 36679
diff changeset
     2
 * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3861
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3861
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3861
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3861
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3861
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util.logging;
29117
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
    27
import java.time.Instant;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
2632
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
    29
import java.util.concurrent.atomic.AtomicInteger;
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
    30
import java.util.concurrent.atomic.AtomicLong;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
34723
734a1c90ce86 8145686: SimpleConsoleLogger and LogRecord should take advantage of StackWalker to skip classes implementing System.Logger
dfuchs
parents: 34439
diff changeset
    32
import java.security.AccessController;
734a1c90ce86 8145686: SimpleConsoleLogger and LogRecord should take advantage of StackWalker to skip classes implementing System.Logger
dfuchs
parents: 34439
diff changeset
    33
import java.security.PrivilegedAction;
29117
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
    34
import java.time.Clock;
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
    35
import java.util.function.Predicate;
36679
0e6911cee995 8150840: Add an internal system property to control the default level of System.Logger when java.logging is not present.
dfuchs
parents: 35302
diff changeset
    36
import static jdk.internal.logger.SurrogateLogger.isFilteredFrame;
2947
b0135c99348e 6511515: poor performance of LogRecord.inferCaller depending on java.lang.Throwable.getStackTraceElement
martin
parents: 2632
diff changeset
    37
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * LogRecord objects are used to pass logging requests between
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * the logging framework and individual log Handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * When a LogRecord is passed into the logging framework it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * logically belongs to the framework and should no longer be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * used or updated by the client application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Note that if the client application has not specified an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * explicit source method name and source class name, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * LogRecord class will infer them automatically when they are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * first accessed (due to a call on getSourceMethodName or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * getSourceClassName) by analyzing the call stack.  Therefore,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * if a logging Handler wants to pass off a LogRecord to another
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * thread, or to transmit it over RMI, and if it wishes to subsequently
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * obtain method name or class name information it should call
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * one of getSourceClassName or getSourceMethodName to force
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * the values to be filled in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <b> Serialization notes:</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <li>The LogRecord class is serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <li> Because objects in the parameters array may not be serializable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * during serialization all objects in the parameters array are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * written as the corresponding Strings (using Object.toString).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <li> The ResourceBundle is not transmitted as part of the serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * form, but the resource bundle name is, and the recipient object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * readObject method will attempt to locate a suitable resource bundle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
public class LogRecord implements java.io.Serializable {
2632
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
    75
    private static final AtomicLong globalSequenceNumber
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
    76
        = new AtomicLong(0);
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
    77
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
    78
    /**
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
    79
     * The default value of threadID will be the current thread's
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
    80
     * thread id, for ease of correlation, unless it is greater than
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
    81
     * MIN_SEQUENTIAL_THREAD_ID, in which case we try harder to keep
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
    82
     * our promise to keep threadIDs unique by avoiding collisions due
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
    83
     * to 32-bit wraparound.  Unfortunately, LogRecord.getThreadID()
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
    84
     * returns int, while Thread.getId() returns long.
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
    85
     */
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
    86
    private static final int MIN_SEQUENTIAL_THREAD_ID = Integer.MAX_VALUE / 2;
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
    87
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
    88
    private static final AtomicInteger nextThreadId
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
    89
        = new AtomicInteger(MIN_SEQUENTIAL_THREAD_ID);
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
    90
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7026
diff changeset
    91
    private static final ThreadLocal<Integer> threadIds = new ThreadLocal<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
29117
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
    94
     * Logging message level
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private Level level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
29117
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
    99
     * Sequence number
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private long sequenceNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
29117
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   104
     * Class that issued logging call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private String sourceClassName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
29117
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   109
     * Method that issued logging call
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private String sourceMethodName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
29117
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   114
     * Non-localized raw message text
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private String message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
29117
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   119
     * Thread ID for thread that issued logging call.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private int threadID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
29117
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   124
     * The Throwable (if any) associated with log message
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private Throwable thrown;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
29117
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   129
     * Name of the source Logger.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private String loggerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
29117
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   134
     * Resource bundle name to localized log message.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private String resourceBundleName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
29117
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   138
    /**
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   139
     * Event time.
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 34723
diff changeset
   140
     * @since 9
29117
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   141
     */
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   142
    private Instant instant;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   143
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   144
    /**
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   145
     * @serialField level Level Logging message level
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   146
     * @serialField sequenceNumber long Sequence number
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   147
     * @serialField sourceClassName String Class that issued logging call
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   148
     * @serialField sourceMethodName String Method that issued logging call
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   149
     * @serialField message String Non-localized raw message text
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   150
     * @serialField threadID int Thread ID for thread that issued logging call
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   151
     * @serialField millis long Truncated event time in milliseconds since 1970
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   152
     *              - calculated as getInstant().toEpochMilli().
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   153
     *               The event time instant can be reconstructed using
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   154
     * <code>Instant.ofEpochSecond(millis/1000, (millis % 1000) * 1000_000 + nanoAdjustment)</code>
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   155
     * @serialField nanoAdjustment int Nanoseconds adjustment to the millisecond of
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   156
     *              event time - calculated as getInstant().getNano() % 1000_000
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   157
     *               The event time instant can be reconstructed using
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   158
     * <code>Instant.ofEpochSecond(millis/1000, (millis % 1000) * 1000_000 + nanoAdjustment)</code>
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   159
     *              <p>
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 34723
diff changeset
   160
     *              Since: 9
29117
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   161
     * @serialField thrown Throwable The Throwable (if any) associated with log
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   162
     *              message
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   163
     * @serialField loggerName String Name of the source Logger
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   164
     * @serialField resourceBundleName String Resource bundle name to localized
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   165
     *              log message
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   166
     */
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   167
    private static final ObjectStreamField[] serialPersistentFields =
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   168
        new ObjectStreamField[] {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   169
            new ObjectStreamField("level", Level.class),
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   170
            new ObjectStreamField("sequenceNumber", long.class),
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   171
            new ObjectStreamField("sourceClassName", String.class),
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   172
            new ObjectStreamField("sourceMethodName", String.class),
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   173
            new ObjectStreamField("message", String.class),
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   174
            new ObjectStreamField("threadID", int.class),
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   175
            new ObjectStreamField("millis", long.class),
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   176
            new ObjectStreamField("nanoAdjustment", int.class),
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   177
            new ObjectStreamField("thrown", Throwable.class),
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   178
            new ObjectStreamField("loggerName", String.class),
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   179
            new ObjectStreamField("resourceBundleName", String.class),
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   180
        };
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   181
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private transient boolean needToInferCaller;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    private transient Object parameters[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    private transient ResourceBundle resourceBundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
2632
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
   187
     * Returns the default value for a new LogRecord's threadID.
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
   188
     */
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
   189
    private int defaultThreadID() {
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
   190
        long tid = Thread.currentThread().getId();
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
   191
        if (tid < MIN_SEQUENTIAL_THREAD_ID) {
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
   192
            return (int) tid;
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
   193
        } else {
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
   194
            Integer id = threadIds.get();
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
   195
            if (id == null) {
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
   196
                id = nextThreadId.getAndIncrement();
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
   197
                threadIds.set(id);
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
   198
            }
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
   199
            return id;
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
   200
        }
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
   201
    }
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
   202
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
   203
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Construct a LogRecord with the given level and message values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * The sequence property will be initialized with a new unique value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * These sequence values are allocated in increasing order within a VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * <p>
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 34723
diff changeset
   209
     * Since JDK 9, the event time is represented by an {@link Instant}.
29117
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   210
     * The instant property will be initialized to the {@linkplain
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   211
     * Instant#now() current instant}, using the best available
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   212
     * {@linkplain Clock#systemUTC() clock} on the system.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * The thread ID property will be initialized with a unique ID for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * the current thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * All other properties will be initialized to "null".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @param level  a logging level value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @param msg  the raw non-localized logging message (may be null)
29117
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   221
     * @see java.time.Clock#systemUTC()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    public LogRecord(Level level, String msg) {
29094
a4fd2b5e49f8 8073479: Replace obj.getClass hacks with Objects.requireNonNull
shade
parents: 27080
diff changeset
   224
        this.level = Objects.requireNonNull(level);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        message = msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        // Assign a thread ID and a unique sequence number.
2632
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
   227
        sequenceNumber = globalSequenceNumber.getAndIncrement();
9779b9cbae42 6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents: 2
diff changeset
   228
        threadID = defaultThreadID();
29117
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   229
        instant = Instant.now();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        needToInferCaller = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
3853
9d2382b74894 6882363: 4/4 typos in java.util.logging javadocs
dcubed
parents: 2947
diff changeset
   234
     * Get the source Logger's name.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @return source logger name (may be null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public String getLoggerName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return loggerName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
3853
9d2382b74894 6882363: 4/4 typos in java.util.logging javadocs
dcubed
parents: 2947
diff changeset
   243
     * Set the source Logger's name.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @param name   the source logger name (may be null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    public void setLoggerName(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        loggerName = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * Get the localization resource bundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * This is the ResourceBundle that should be used to localize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * the message string before formatting it.  The result may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * be null if the message is not localizable, or if no suitable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * ResourceBundle is available.
18565
5025d43d3731 8019315: Fix doclint issues in java.util.logging
darcy
parents: 7816
diff changeset
   258
     * @return the localization resource bundle
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public ResourceBundle getResourceBundle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        return resourceBundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * Set the localization resource bundle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @param bundle  localization bundle (may be null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    public void setResourceBundle(ResourceBundle bundle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        resourceBundle = bundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * Get the localization resource bundle name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * This is the name for the ResourceBundle that should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * used to localize the message string before formatting it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * The result may be null if the message is not localizable.
18565
5025d43d3731 8019315: Fix doclint issues in java.util.logging
darcy
parents: 7816
diff changeset
   279
     * @return the localization resource bundle name
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    public String getResourceBundleName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        return resourceBundleName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * Set the localization resource bundle name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @param name  localization bundle name (may be null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    public void setResourceBundleName(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        resourceBundleName = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Get the logging message level, for example Level.SEVERE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @return the logging message level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public Level getLevel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        return level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * Set the logging message level, for example Level.SEVERE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @param level the logging message level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    public void setLevel(Level level) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        if (level == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        this.level = level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * Get the sequence number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Sequence numbers are normally assigned in the LogRecord
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * constructor, which assigns unique sequence numbers to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * each new LogRecord in increasing order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @return the sequence number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    public long getSequenceNumber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        return sequenceNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * Set the sequence number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Sequence numbers are normally assigned in the LogRecord constructor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * so it should not normally be necessary to use this method.
18565
5025d43d3731 8019315: Fix doclint issues in java.util.logging
darcy
parents: 7816
diff changeset
   330
     * @param seq the sequence number
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    public void setSequenceNumber(long seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        sequenceNumber = seq;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * Get the  name of the class that (allegedly) issued the logging request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * Note that this sourceClassName is not verified and may be spoofed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * This information may either have been provided as part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * logging call, or it may have been inferred automatically by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * logging framework.  In the latter case, the information may only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * be approximate and may in fact describe an earlier call on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * stack frame.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * May be null if no information could be obtained.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @return the source class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    public String getSourceClassName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        if (needToInferCaller) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            inferCaller();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        return sourceClassName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * Set the name of the class that (allegedly) issued the logging request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @param sourceClassName the source class name (may be null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    public void setSourceClassName(String sourceClassName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        this.sourceClassName = sourceClassName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        needToInferCaller = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * Get the  name of the method that (allegedly) issued the logging request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * Note that this sourceMethodName is not verified and may be spoofed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * This information may either have been provided as part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * logging call, or it may have been inferred automatically by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * logging framework.  In the latter case, the information may only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * be approximate and may in fact describe an earlier call on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * stack frame.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * May be null if no information could be obtained.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @return the source method name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    public String getSourceMethodName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        if (needToInferCaller) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            inferCaller();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        return sourceMethodName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * Set the name of the method that (allegedly) issued the logging request.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @param sourceMethodName the source method name (may be null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    public void setSourceMethodName(String sourceMethodName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        this.sourceMethodName = sourceMethodName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        needToInferCaller = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * Get the "raw" log message, before localization or formatting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * May be null, which is equivalent to the empty string "".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * This message may be either the final text or a localization key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * During formatting, if the source logger has a localization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * ResourceBundle and if that ResourceBundle has an entry for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * this message string, then the message string is replaced
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * with the localized value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @return the raw message string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    public String getMessage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        return message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * Set the "raw" log message, before localization or formatting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * @param message the raw message string (may be null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    public void setMessage(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        this.message = message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * Get the parameters to the log message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @return the log message parameters.  May be null if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *                  there are no parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    public Object[] getParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        return parameters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * Set the parameters to the log message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * @param parameters the log message parameters. (may be null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    public void setParameters(Object parameters[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        this.parameters = parameters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * Get an identifier for the thread where the message originated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * This is a thread identifier within the Java VM and may or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * may not map to any operating system ID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @return thread ID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    public int getThreadID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        return threadID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * Set an identifier for the thread where the message originated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @param threadID  the thread ID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    public void setThreadID(int threadID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        this.threadID = threadID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    /**
29117
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   465
     * Get truncated event time in milliseconds since 1970.
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   466
     *
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   467
     * @return truncated event time in millis since 1970
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   468
     *
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   469
     * @implSpec This is equivalent to calling
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   470
     *      {@link #getInstant() getInstant().toEpochMilli()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     *
34439
fbb6e8d9611c 8144262: LogRecord.getMillis() method is a convenience API that should not have been deprecated
dfuchs
parents: 34372
diff changeset
   472
     * @apiNote To get the full nanosecond resolution event time,
29117
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   473
     *             use {@link #getInstant()}.
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   474
     *
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   475
     * @see #getInstant()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    public long getMillis() {
29117
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   478
        return instant.toEpochMilli();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * Set event time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *
29117
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   484
     * @param millis event time in millis since 1970.
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   485
     *
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   486
     * @implSpec This is equivalent to calling
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   487
     *      {@link #setInstant(java.time.Instant)
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   488
     *      setInstant(Instant.ofEpochMilli(millis))}.
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   489
     *
34439
fbb6e8d9611c 8144262: LogRecord.getMillis() method is a convenience API that should not have been deprecated
dfuchs
parents: 34372
diff changeset
   490
     * @deprecated LogRecord maintains timestamps with nanosecond resolution,
fbb6e8d9611c 8144262: LogRecord.getMillis() method is a convenience API that should not have been deprecated
dfuchs
parents: 34372
diff changeset
   491
     *             using {@link Instant} values. For this reason,
fbb6e8d9611c 8144262: LogRecord.getMillis() method is a convenience API that should not have been deprecated
dfuchs
parents: 34372
diff changeset
   492
     *             {@link #setInstant(java.time.Instant) setInstant()}
fbb6e8d9611c 8144262: LogRecord.getMillis() method is a convenience API that should not have been deprecated
dfuchs
parents: 34372
diff changeset
   493
     *             should be used in preference to {@code setMillis()}.
29117
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   494
     *
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   495
     * @see #setInstant(java.time.Instant)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     */
29117
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   497
    @Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    public void setMillis(long millis) {
29117
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   499
        this.instant = Instant.ofEpochMilli(millis);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   500
    }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   501
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   502
    /**
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   503
     * Gets the instant that the event occurred.
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   504
     *
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   505
     * @return the instant that the event occurred.
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   506
     *
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 34723
diff changeset
   507
     * @since 9
29117
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   508
     */
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   509
    public Instant getInstant() {
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   510
        return instant;
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   511
    }
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   512
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   513
    /**
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   514
     * Sets the instant that the event occurred.
34439
fbb6e8d9611c 8144262: LogRecord.getMillis() method is a convenience API that should not have been deprecated
dfuchs
parents: 34372
diff changeset
   515
     * <p>
fbb6e8d9611c 8144262: LogRecord.getMillis() method is a convenience API that should not have been deprecated
dfuchs
parents: 34372
diff changeset
   516
     * If the given {@code instant} represents a point on the time-line too
fbb6e8d9611c 8144262: LogRecord.getMillis() method is a convenience API that should not have been deprecated
dfuchs
parents: 34372
diff changeset
   517
     * far in the future or past to fit in a {@code long} milliseconds and
fbb6e8d9611c 8144262: LogRecord.getMillis() method is a convenience API that should not have been deprecated
dfuchs
parents: 34372
diff changeset
   518
     * nanoseconds adjustment, then an {@code ArithmeticException} will be
fbb6e8d9611c 8144262: LogRecord.getMillis() method is a convenience API that should not have been deprecated
dfuchs
parents: 34372
diff changeset
   519
     * thrown.
29117
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   520
     *
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   521
     * @param instant the instant that the event occurred.
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   522
     *
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   523
     * @throws NullPointerException if {@code instant} is null.
34439
fbb6e8d9611c 8144262: LogRecord.getMillis() method is a convenience API that should not have been deprecated
dfuchs
parents: 34372
diff changeset
   524
     * @throws ArithmeticException if numeric overflow would occur while
fbb6e8d9611c 8144262: LogRecord.getMillis() method is a convenience API that should not have been deprecated
dfuchs
parents: 34372
diff changeset
   525
     *         calling {@link Instant#toEpochMilli() instant.toEpochMilli()}.
fbb6e8d9611c 8144262: LogRecord.getMillis() method is a convenience API that should not have been deprecated
dfuchs
parents: 34372
diff changeset
   526
     *
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 34723
diff changeset
   527
     * @since 9
29117
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   528
     */
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   529
    public void setInstant(Instant instant) {
34439
fbb6e8d9611c 8144262: LogRecord.getMillis() method is a convenience API that should not have been deprecated
dfuchs
parents: 34372
diff changeset
   530
        instant.toEpochMilli();
fbb6e8d9611c 8144262: LogRecord.getMillis() method is a convenience API that should not have been deprecated
dfuchs
parents: 34372
diff changeset
   531
        this.instant = instant;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * Get any throwable associated with the log record.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * If the event involved an exception, this will be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * exception object. Otherwise null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @return a throwable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    public Throwable getThrown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        return thrown;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * Set a throwable associated with the log event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * @param thrown  a throwable (may be null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    public void setThrown(Throwable thrown) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        this.thrown = thrown;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    private static final long serialVersionUID = 5372048053134512534L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    /**
29117
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   558
     * @serialData Serialized fields, followed by a two byte version number
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * (major byte, followed by minor byte), followed by information on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * the log record parameter array.  If there is no parameter array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * then -1 is written.  If there is a parameter array (possible of zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * length) then the array length is written as an integer, followed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * by String values for each parameter.  If a parameter is null, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * a null String is written.  Otherwise the output of Object.toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * is written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    private void writeObject(ObjectOutputStream out) throws IOException {
29117
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   568
        // We have to write serialized fields first.
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   569
        ObjectOutputStream.PutField pf = out.putFields();
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   570
        pf.put("level", level);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   571
        pf.put("sequenceNumber", sequenceNumber);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   572
        pf.put("sourceClassName", sourceClassName);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   573
        pf.put("sourceMethodName", sourceMethodName);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   574
        pf.put("message", message);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   575
        pf.put("threadID", threadID);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   576
        pf.put("millis", instant.toEpochMilli());
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   577
        pf.put("nanoAdjustment", instant.getNano() % 1000_000);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   578
        pf.put("thrown", thrown);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   579
        pf.put("loggerName", loggerName);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   580
        pf.put("resourceBundleName", resourceBundleName);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   581
        out.writeFields();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        // Write our version number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        out.writeByte(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        out.writeByte(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        if (parameters == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            out.writeInt(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        out.writeInt(parameters.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        // Write string values for the parameters.
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 18565
diff changeset
   592
        for (Object parameter : parameters) {
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 18565
diff changeset
   593
            out.writeObject(Objects.toString(parameter, null));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    private void readObject(ObjectInputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                        throws IOException, ClassNotFoundException {
29117
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   599
        // We have to read serialized fields first.
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   600
        ObjectInputStream.GetField gf = in.readFields();
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   601
        level = (Level) gf.get("level", null);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   602
        sequenceNumber = gf.get("sequenceNumber", 0L);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   603
        sourceClassName = (String) gf.get("sourceClassName", null);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   604
        sourceMethodName = (String) gf.get("sourceMethodName", null);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   605
        message = (String) gf.get("message", null);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   606
        threadID = gf.get("threadID", 0);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   607
        long millis = gf.get("millis", 0L);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   608
        int nanoOfMilli = gf.get("nanoAdjustment", 0);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   609
        instant = Instant.ofEpochSecond(
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   610
            millis / 1000L, (millis % 1000L) * 1000_000L + nanoOfMilli);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   611
        thrown = (Throwable) gf.get("thrown", null);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   612
        loggerName = (String) gf.get("loggerName", null);
7956b5dc0eac 8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents: 29094
diff changeset
   613
        resourceBundleName = (String) gf.get("resourceBundleName", null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        // Read version number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        byte major = in.readByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        byte minor = in.readByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        if (major != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            throw new IOException("LogRecord: bad version: " + major + "." + minor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        int len = in.readInt();
43197
883675f526b8 8162577: Standardize logging levels
dfuchs
parents: 36679
diff changeset
   622
        if (len < -1) {
883675f526b8 8162577: Standardize logging levels
dfuchs
parents: 36679
diff changeset
   623
            throw new NegativeArraySizeException();
883675f526b8 8162577: Standardize logging levels
dfuchs
parents: 36679
diff changeset
   624
        } else if (len == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            parameters = null;
43197
883675f526b8 8162577: Standardize logging levels
dfuchs
parents: 36679
diff changeset
   626
        } else if (len < 255) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            parameters = new Object[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            for (int i = 0; i < parameters.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                parameters[i] = in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            }
43197
883675f526b8 8162577: Standardize logging levels
dfuchs
parents: 36679
diff changeset
   631
        } else {
883675f526b8 8162577: Standardize logging levels
dfuchs
parents: 36679
diff changeset
   632
            List<Object> params = new ArrayList<>(Math.min(len, 1024));
883675f526b8 8162577: Standardize logging levels
dfuchs
parents: 36679
diff changeset
   633
            for (int i = 0; i < len; i++) {
883675f526b8 8162577: Standardize logging levels
dfuchs
parents: 36679
diff changeset
   634
                params.add(in.readObject());
883675f526b8 8162577: Standardize logging levels
dfuchs
parents: 36679
diff changeset
   635
            }
883675f526b8 8162577: Standardize logging levels
dfuchs
parents: 36679
diff changeset
   636
            parameters = params.toArray(new Object[params.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        // If necessary, try to regenerate the resource bundle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        if (resourceBundleName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            try {
27080
4c4cdbcd69b5 8042797: Avoid strawberries in LogRecord
dfuchs
parents: 25859
diff changeset
   641
                // use system class loader to ensure the ResourceBundle
4c4cdbcd69b5 8042797: Avoid strawberries in LogRecord
dfuchs
parents: 25859
diff changeset
   642
                // instance is a different instance than null loader uses
4c4cdbcd69b5 8042797: Avoid strawberries in LogRecord
dfuchs
parents: 25859
diff changeset
   643
                final ResourceBundle bundle =
4c4cdbcd69b5 8042797: Avoid strawberries in LogRecord
dfuchs
parents: 25859
diff changeset
   644
                        ResourceBundle.getBundle(resourceBundleName,
4c4cdbcd69b5 8042797: Avoid strawberries in LogRecord
dfuchs
parents: 25859
diff changeset
   645
                                Locale.getDefault(),
4c4cdbcd69b5 8042797: Avoid strawberries in LogRecord
dfuchs
parents: 25859
diff changeset
   646
                                ClassLoader.getSystemClassLoader());
4c4cdbcd69b5 8042797: Avoid strawberries in LogRecord
dfuchs
parents: 25859
diff changeset
   647
                resourceBundle = bundle;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            } catch (MissingResourceException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                // This is not a good place to throw an exception,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                // so we simply leave the resourceBundle null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                resourceBundle = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        needToInferCaller = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    // Private method to infer the caller's class and method names
33875
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32834
diff changeset
   659
    //
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32834
diff changeset
   660
    // Note:
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32834
diff changeset
   661
    // For testing purposes - it is possible to customize the process
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32834
diff changeset
   662
    // by which LogRecord will infer the source class name and source method name
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32834
diff changeset
   663
    // when analyzing the call stack.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32834
diff changeset
   664
    // <p>
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32834
diff changeset
   665
    // The system property {@code jdk.logger.packages} can define a comma separated
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32834
diff changeset
   666
    // list of strings corresponding to additional package name prefixes that
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32834
diff changeset
   667
    // should be ignored when trying to infer the source caller class name.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32834
diff changeset
   668
    // Those stack frames whose {@linkplain StackTraceElement#getClassName()
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32834
diff changeset
   669
    // declaring class name} start with one such prefix will be ignored.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32834
diff changeset
   670
    // <p>
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32834
diff changeset
   671
    // This is primarily useful when providing utility logging classes wrapping
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32834
diff changeset
   672
    // a logger instance, as it makes it possible to instruct LogRecord to skip
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32834
diff changeset
   673
    // those utility frames when inferring the caller source class name.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32834
diff changeset
   674
    // <p>
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32834
diff changeset
   675
    // The {@code jdk.logger.packages} system property is consulted only once.
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32834
diff changeset
   676
    // <p>
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32834
diff changeset
   677
    // This property is not standard, implementation specific, and yet
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32834
diff changeset
   678
    // undocumented (and thus subject to changes without notice).
c1c71107d45f 8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents: 32834
diff changeset
   679
    //
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    private void inferCaller() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        needToInferCaller = false;
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   682
        // Skip all frames until we have found the first logger frame.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   683
        Optional<StackWalker.StackFrame> frame = new CallerFinder().get();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   684
        frame.ifPresent(f -> {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   685
            setSourceClassName(f.getClassName());
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   686
            setSourceMethodName(f.getMethodName());
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   687
        });
2947
b0135c99348e 6511515: poor performance of LogRecord.inferCaller depending on java.lang.Throwable.getStackTraceElement
martin
parents: 2632
diff changeset
   688
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        // We haven't found a suitable frame, so just punt.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        // OK as we are only committed to making a "best effort" here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    }
7026
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents: 5506
diff changeset
   692
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   693
    /*
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   694
     * CallerFinder is a stateful predicate.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   695
     */
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   696
    static final class CallerFinder implements Predicate<StackWalker.StackFrame> {
34723
734a1c90ce86 8145686: SimpleConsoleLogger and LogRecord should take advantage of StackWalker to skip classes implementing System.Logger
dfuchs
parents: 34439
diff changeset
   697
        private static final StackWalker WALKER;
734a1c90ce86 8145686: SimpleConsoleLogger and LogRecord should take advantage of StackWalker to skip classes implementing System.Logger
dfuchs
parents: 34439
diff changeset
   698
        static {
734a1c90ce86 8145686: SimpleConsoleLogger and LogRecord should take advantage of StackWalker to skip classes implementing System.Logger
dfuchs
parents: 34439
diff changeset
   699
            final PrivilegedAction<StackWalker> action =
734a1c90ce86 8145686: SimpleConsoleLogger and LogRecord should take advantage of StackWalker to skip classes implementing System.Logger
dfuchs
parents: 34439
diff changeset
   700
                    () -> StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
734a1c90ce86 8145686: SimpleConsoleLogger and LogRecord should take advantage of StackWalker to skip classes implementing System.Logger
dfuchs
parents: 34439
diff changeset
   701
            WALKER = AccessController.doPrivileged(action);
734a1c90ce86 8145686: SimpleConsoleLogger and LogRecord should take advantage of StackWalker to skip classes implementing System.Logger
dfuchs
parents: 34439
diff changeset
   702
        }
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   703
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   704
        /**
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   705
         * Returns StackFrame of the caller's frame.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   706
         * @return StackFrame of the caller's frame.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   707
         */
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   708
        Optional<StackWalker.StackFrame> get() {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   709
            return WALKER.walk((s) -> s.filter(this).findFirst());
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   710
        }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   711
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   712
        private boolean lookingForLogger = true;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   713
        /**
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   714
         * Returns true if we have found the caller's frame, false if the frame
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   715
         * must be skipped.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   716
         *
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   717
         * @param t The frame info.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   718
         * @return true if we have found the caller's frame, false if the frame
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   719
         * must be skipped.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   720
         */
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   721
        @Override
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   722
        public boolean test(StackWalker.StackFrame t) {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   723
            final String cname = t.getClassName();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   724
            // We should skip all frames until we have found the logger,
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   725
            // because these frames could be frames introduced by e.g. custom
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   726
            // sub classes of Handler.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   727
            if (lookingForLogger) {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   728
                // the log record could be created for a platform logger
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   729
                lookingForLogger = !isLoggerImplFrame(cname);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   730
                return false;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   731
            }
34723
734a1c90ce86 8145686: SimpleConsoleLogger and LogRecord should take advantage of StackWalker to skip classes implementing System.Logger
dfuchs
parents: 34439
diff changeset
   732
            // Continue walking until we've found the relevant calling frame.
734a1c90ce86 8145686: SimpleConsoleLogger and LogRecord should take advantage of StackWalker to skip classes implementing System.Logger
dfuchs
parents: 34439
diff changeset
   733
            // Skips logging/logger infrastructure.
734a1c90ce86 8145686: SimpleConsoleLogger and LogRecord should take advantage of StackWalker to skip classes implementing System.Logger
dfuchs
parents: 34439
diff changeset
   734
            return !isFilteredFrame(t);
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   735
        }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   736
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   737
        private boolean isLoggerImplFrame(String cname) {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   738
            return (cname.equals("java.util.logging.Logger") ||
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   739
                    cname.startsWith("sun.util.logging.PlatformLogger"));
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents: 34370
diff changeset
   740
        }
7026
9f2ec5fad124 6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents: 5506
diff changeset
   741
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
}