@@ -33,6 +33,17 @@ enum class Severity(val sev: Int) {
3333 ErrorGlobal (8 )
3434}
3535
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+
3647data class ExtractorContext (val kind : String , val element : IrElement , val name : String , val loc : String )
3748
3849open class LoggerBase (val logCounter : LogCounter ) {
@@ -54,10 +65,6 @@ open class LoggerBase(val logCounter: LogCounter) {
5465 }
5566 }
5667
57- private fun timestamp (): String {
58- return " [${SimpleDateFormat (" yyyy-MM-dd HH:mm:ss" ).format(Date ())} K]"
59- }
60-
6168 private fun getDiagnosticLocation (): String? {
6269 val st = Exception ().stackTrace
6370 for (x in st) {
@@ -84,7 +91,6 @@ open class LoggerBase(val logCounter: LogCounter) {
8491 fun diagnostic (tw : TrapWriter , severity : Severity , msg : String , extraInfo : String? , locationString : String? = null, mkLocationId : () -> Label <DbLocation > = { tw.unknownLocation }) {
8592 val diagnosticLoc = getDiagnosticLocation()
8693 val diagnosticLocStr = if (diagnosticLoc == null ) " <unknown location>" else diagnosticLoc
87- val extraInfoStr = if (extraInfo == null ) " " else (extraInfo + " \n " )
8894 val suffix =
8995 if (diagnosticLoc == null ) {
9096 " Missing caller information.\n "
@@ -100,8 +106,10 @@ open class LoggerBase(val logCounter: LogCounter) {
100106 }
101107 val fullMsgBuilder = StringBuilder ()
102108 fullMsgBuilder.append(msg)
103- fullMsgBuilder.append(' \n ' )
104- fullMsgBuilder.append(extraInfoStr)
109+ if (extraInfo != null ) {
110+ fullMsgBuilder.append(' \n ' )
111+ fullMsgBuilder.append(extraInfo)
112+ }
105113
106114 val iter = extractorContextStack.listIterator(extractorContextStack.size)
107115 while (iter.hasPrevious()) {
@@ -111,38 +119,38 @@ open class LoggerBase(val logCounter: LogCounter) {
111119 fullMsgBuilder.append(suffix)
112120
113121 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 " )
115125 // We don't actually make the location until after the `return` above
116126 val locationId = mkLocationId()
117127 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)
119129 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 " )
123131 }
124132
125133 fun trace (tw : TrapWriter , msg : String ) {
126134 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 " )
130138 }
131139 }
132140
133141 fun debug (tw : TrapWriter , msg : String ) {
134142 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 " )
138146 }
139147 }
140148
141149 fun info (tw : TrapWriter , msg : String ) {
142150 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 " )
146154 }
147155 }
148156
@@ -160,9 +168,12 @@ open class LoggerBase(val logCounter: LogCounter) {
160168 fun printLimitedDiagnosticCounts (tw : TrapWriter ) {
161169 for ((caller, count) in logCounter.diagnosticCounts) {
162170 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 " )
166177 }
167178 }
168179 }
0 commit comments