@@ -33,6 +33,17 @@ enum class Severity(val sev: Int) {
33
33
ErrorGlobal (8 )
34
34
}
35
35
36
+ class LogMessage (private val kind : String , private val message : String ) {
37
+ val timestamp: String
38
+ init {
39
+ timestamp = " ${SimpleDateFormat (" yyyy-MM-dd HH:mm:ss" ).format(Date ())} "
40
+ }
41
+
42
+ fun toText (): String {
43
+ return " [$timestamp K] [$kind ] $message "
44
+ }
45
+ }
46
+
36
47
data class ExtractorContext (val kind : String , val element : IrElement , val name : String , val loc : String )
37
48
38
49
open class LoggerBase (val logCounter : LogCounter ) {
@@ -54,10 +65,6 @@ open class LoggerBase(val logCounter: LogCounter) {
54
65
}
55
66
}
56
67
57
- private fun timestamp (): String {
58
- return " [${SimpleDateFormat (" yyyy-MM-dd HH:mm:ss" ).format(Date ())} K]"
59
- }
60
-
61
68
private fun getDiagnosticLocation (): String? {
62
69
val st = Exception ().stackTrace
63
70
for (x in st) {
@@ -84,7 +91,6 @@ open class LoggerBase(val logCounter: LogCounter) {
84
91
fun diagnostic (tw : TrapWriter , severity : Severity , msg : String , extraInfo : String? , locationString : String? = null, mkLocationId : () -> Label <DbLocation > = { tw.unknownLocation }) {
85
92
val diagnosticLoc = getDiagnosticLocation()
86
93
val diagnosticLocStr = if (diagnosticLoc == null ) " <unknown location>" else diagnosticLoc
87
- val extraInfoStr = if (extraInfo == null ) " " else (extraInfo + " \n " )
88
94
val suffix =
89
95
if (diagnosticLoc == null ) {
90
96
" Missing caller information.\n "
@@ -100,8 +106,10 @@ open class LoggerBase(val logCounter: LogCounter) {
100
106
}
101
107
val fullMsgBuilder = StringBuilder ()
102
108
fullMsgBuilder.append(msg)
103
- fullMsgBuilder.append(' \n ' )
104
- fullMsgBuilder.append(extraInfoStr)
109
+ if (extraInfo != null ) {
110
+ fullMsgBuilder.append(' \n ' )
111
+ fullMsgBuilder.append(extraInfo)
112
+ }
105
113
106
114
val iter = extractorContextStack.listIterator(extractorContextStack.size)
107
115
while (iter.hasPrevious()) {
@@ -111,38 +119,38 @@ open class LoggerBase(val logCounter: LogCounter) {
111
119
fullMsgBuilder.append(suffix)
112
120
113
121
val fullMsg = fullMsgBuilder.toString()
114
- val ts = timestamp()
122
+ val locStr = if (locationString == null ) " " else " At " + locationString + " : "
123
+ val kind = if (severity <= Severity .WarnHigh ) " WARN" else " ERROR"
124
+ val logMessage = LogMessage (kind, " Diagnostic($diagnosticLocStr ): $locStr$fullMsg " )
115
125
// We don't actually make the location until after the `return` above
116
126
val locationId = mkLocationId()
117
127
val diagLabel = tw.getFreshIdLabel<DbDiagnostic >()
118
- tw.writeDiagnostics(diagLabel, " CodeQL Kotlin extractor" , severity.sev, " " , msg, " $ts $fullMsg " , locationId)
128
+ tw.writeDiagnostics(diagLabel, " CodeQL Kotlin extractor" , severity.sev, " " , msg, " ${logMessage.timestamp} $fullMsg " , locationId)
119
129
tw.writeDiagnostic_for(diagLabel, StringLabel (" compilation" ), file_number, file_number_diagnostic_number++ )
120
- val locStr = if (locationString == null ) " " else " At " + locationString + " : "
121
- val kind = if (severity <= Severity .WarnHigh ) " WARN" else " ERROR"
122
- logStream.write(" $ts [$kind ] Diagnostic($diagnosticLocStr ): $locStr$fullMsg " )
130
+ logStream.write(logMessage.toText() + " \n " )
123
131
}
124
132
125
133
fun trace (tw : TrapWriter , msg : String ) {
126
134
if (verbosity >= 4 ) {
127
- val fullMsg = " ${timestamp()} [ TRACE] $ msg"
128
- tw.writeComment(fullMsg )
129
- logStream.write(fullMsg + " \n " )
135
+ val logMessage = LogMessage ( " TRACE" , msg)
136
+ tw.writeComment(logMessage.toText() )
137
+ logStream.write(logMessage.toText() + " \n " )
130
138
}
131
139
}
132
140
133
141
fun debug (tw : TrapWriter , msg : String ) {
134
142
if (verbosity >= 4 ) {
135
- val fullMsg = " ${timestamp()} [ DEBUG] $ msg"
136
- tw.writeComment(fullMsg )
137
- logStream.write(fullMsg + " \n " )
143
+ val logMessage = LogMessage ( " DEBUG" , msg)
144
+ tw.writeComment(logMessage.toText() )
145
+ logStream.write(logMessage.toText() + " \n " )
138
146
}
139
147
}
140
148
141
149
fun info (tw : TrapWriter , msg : String ) {
142
150
if (verbosity >= 3 ) {
143
- val fullMsg = " ${timestamp()} [ INFO] $ msg"
144
- tw.writeComment(fullMsg )
145
- logStream.write(fullMsg + " \n " )
151
+ val logMessage = LogMessage ( " INFO" , msg)
152
+ tw.writeComment(logMessage.toText() )
153
+ logStream.write(logMessage.toText() + " \n " )
146
154
}
147
155
}
148
156
@@ -160,9 +168,12 @@ open class LoggerBase(val logCounter: LogCounter) {
160
168
fun printLimitedDiagnosticCounts (tw : TrapWriter ) {
161
169
for ((caller, count) in logCounter.diagnosticCounts) {
162
170
if (count >= logCounter.diagnosticLimit) {
163
- val msg = " Total of $count diagnostics from $caller .\n "
164
- tw.writeComment(msg)
165
- logStream.write(msg)
171
+ // We don't know if this location relates to an error
172
+ // or a warning, so we just declare hitting the limit
173
+ // to be an error regardless.
174
+ val logMessage = LogMessage (" ERROR" , " Total of $count diagnostics from $caller ." )
175
+ tw.writeComment(logMessage.toText())
176
+ logStream.write(logMessage.toText() + " \n " )
166
177
}
167
178
}
168
179
}
0 commit comments