| 
									
										
										
										
											2021-08-21 23:26:03 +00:00
										 |  |  | import * as core from '@actions/core'; | 
					
						
							| 
									
										
										
										
											2022-01-02 00:33:18 +00:00
										 |  |  | import { exec } from 'child_process'; | 
					
						
							|  |  |  | import { Input } from '../..'; | 
					
						
							| 
									
										
										
										
											2021-08-21 23:26:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-21 18:27:04 +00:00
										 |  |  | class CloudRunnerLogger { | 
					
						
							| 
									
										
										
										
											2021-08-21 23:26:03 +00:00
										 |  |  |   private static timestamp: number; | 
					
						
							|  |  |  |   private static globalTimestamp: number; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   public static setup() { | 
					
						
							|  |  |  |     this.timestamp = this.createTimestamp(); | 
					
						
							|  |  |  |     this.globalTimestamp = this.timestamp; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-21 18:27:04 +00:00
										 |  |  |   public static log(message: string) { | 
					
						
							|  |  |  |     core.info(message); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-31 20:12:47 +00:00
										 |  |  |   public static logWarning(message: string) { | 
					
						
							|  |  |  |     core.warning(message); | 
					
						
							| 
									
										
										
										
											2021-12-29 14:35:13 +00:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-19 01:45:55 +00:00
										 |  |  |   public static logLine(message: string) { | 
					
						
							|  |  |  |     core.info(`${message}\n`); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-21 18:27:04 +00:00
										 |  |  |   public static error(message: string) { | 
					
						
							|  |  |  |     core.error(message); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-21 23:26:03 +00:00
										 |  |  |   public static logWithTime(message: string) { | 
					
						
							|  |  |  |     const newTimestamp = this.createTimestamp(); | 
					
						
							|  |  |  |     core.info( | 
					
						
							|  |  |  |       `${message} (Since previous: ${this.calculateTimeDiff( | 
					
						
							|  |  |  |         newTimestamp, | 
					
						
							|  |  |  |         this.timestamp, | 
					
						
							|  |  |  |       )}, Total time: ${this.calculateTimeDiff(newTimestamp, this.globalTimestamp)})`,
 | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |     this.timestamp = newTimestamp; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private static calculateTimeDiff(x: number, y: number) { | 
					
						
							|  |  |  |     return Math.floor((x - y) / 1000); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private static createTimestamp() { | 
					
						
							|  |  |  |     return Date.now(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-01-01 18:36:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-02 00:33:18 +00:00
										 |  |  |   public static InitHook() { | 
					
						
							|  |  |  |     if (process.env.INIT_HOOK === undefined || !Input.cloudRunnerTests) { | 
					
						
							| 
									
										
										
										
											2022-01-01 18:36:17 +00:00
										 |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-01-02 00:33:18 +00:00
										 |  |  |     exec(process.env.INIT_HOOK, (error: any, stdout: string, stderr: any) => { | 
					
						
							|  |  |  |       if (error) { | 
					
						
							|  |  |  |         CloudRunnerLogger.error(JSON.stringify(error)); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       if (stderr) { | 
					
						
							|  |  |  |         CloudRunnerLogger.logWarning(`[GCP-LOGGER]${stderr}`); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       CloudRunnerLogger.log(stdout); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2022-01-01 18:36:17 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-08-21 23:26:03 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-09-21 18:27:04 +00:00
										 |  |  | export default CloudRunnerLogger; |