You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@daffodil.apache.org by "Shanedell (via GitHub)" <gi...@apache.org> on 2023/04/11 20:13:06 UTC

[GitHub] [daffodil-vscode] Shanedell commented on a diff in pull request #566: upgrade to omega-edit 0.9.60

Shanedell commented on code in PR #566:
URL: https://github.com/apache/daffodil-vscode/pull/566#discussion_r1163275984


##########
src/dataEdit/client.ts:
##########
@@ -0,0 +1,270 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import * as fs from 'fs'
+import {
+  createSimpleFileLogger,
+  getClientVersion,
+  getServerVersion,
+  setAutoFixViewportDataLength,
+  setLogger,
+  startServer,
+  stopServerUsingPID,
+} from '@omega-edit/client'
+import path from 'path'
+import * as vscode from 'vscode'
+import XDGAppPaths from 'xdg-app-paths'
+import { DataEditWebView } from './dataEditWebView'
+import { initOmegaEditClient } from './utils'
+
+export let omegaEditPort: number = 0
+
+const DEFAULT_OMEGA_EDIT_PORT: number = 9000
+const OMEGA_EDIT_MIN_PORT: number = 1024
+const OMEGA_EDIT_MAX_PORT: number = 65535
+const MAX_LOG_FILES = 5 // Maximum number of log files to keep TODO: make this configurable
+const appDataPath = XDGAppPaths({ name: 'omega_edit' }).data()
+
+function rotateLogFiles(logFile: string) {
+  interface LogFile {
+    path: string
+    ctime: Date
+  }
+
+  if (MAX_LOG_FILES <= 0) {
+    throw new Error('Maximum number of log files must be greater than 0')
+  }
+  if (fs.existsSync(logFile)) {
+    const logDir = path.dirname(logFile)
+    const logFileName = path.basename(logFile)
+
+    // Get list of existing log files
+    const logFiles: LogFile[] = fs
+      .readdirSync(logDir)
+      .filter((file) => file.startsWith(logFileName) && file !== logFileName)
+      .map((file) => ({
+        path: path.join(logDir, file),
+        ctime: fs.statSync(path.join(logDir, file)).ctime,
+      }))
+      .sort((a, b) => b.ctime.getTime() - a.ctime.getTime())
+
+    // Delete oldest log files if maximum number of log files is exceeded
+    while (logFiles.length >= MAX_LOG_FILES) {
+      const fileToDelete = logFiles.pop() as LogFile
+      fs.unlinkSync(fileToDelete.path)
+    }
+
+    // Rename current log file with timestamp and create a new empty file
+    const timestamp = new Date().toISOString().replace(/:/g, '-')
+    fs.renameSync(logFile, path.join(logDir, `${logFileName}.${timestamp}`))
+  }
+}
+
+function getPidFile(serverPort: number) {
+  return path.join(appDataPath, `serv-${serverPort}.pid`)
+}
+
+async function getOmegaEditPort() {
+  if (omegaEditPort === 0) {
+    omegaEditPort = vscode.workspace
+      .getConfiguration('dataEditor')
+      .get<number>('omegaEditServerPort', DEFAULT_OMEGA_EDIT_PORT)
+    if (
+      omegaEditPort <= OMEGA_EDIT_MIN_PORT ||
+      omegaEditPort > OMEGA_EDIT_MAX_PORT
+    ) {
+      omegaEditPort = 0
+      throw new Error('Invalid port')
+    }
+  } else
+    throw new Error('Data Editor currently only supports a single instance')
+}
+
+function setupLogging() {
+  const config = vscode.workspace.getConfiguration('dataEditor')
+  const logFile = config
+    .get<string>(
+      'logFile',
+      '${workspaceFolder}/dataEditor-${omegaEditPort}.log'
+    )
+    ?.replace('${workspaceFolder}', appDataPath)
+    .replace('${omegaEditPort}', omegaEditPort.toString())
+  const logLevel =
+    process.env.OMEGA_EDIT_CLIENT_LOG_LEVEL ||
+    config.get<string>('logLevel', 'info')
+  rotateLogFiles(logFile)
+  setLogger(createSimpleFileLogger(logFile, logLevel))
+  vscode.window.showInformationMessage(
+    `Logging to '${logFile}', at level '${logLevel}'`
+  )
+}
+
+async function serverStop(serverPort: number) {
+  const serverPidFile = getPidFile(serverPort)
+  if (fs.existsSync(serverPidFile)) {
+    const pid = parseInt(fs.readFileSync(serverPidFile).toString())
+    if (await stopServerUsingPID(pid)) {
+      vscode.window.setStatusBarMessage(
+        `Ωedit server stopped on port ${omegaEditPort} with PID ${pid}`,
+        new Promise((resolve) => {
+          setTimeout(() => {
+            resolve(true)
+          }, 2000)
+        })
+      )
+    } else {
+      vscode.window.showErrorMessage(
+        `Ωedit server on port ${omegaEditPort} with PID ${pid} failed to stop`
+      )
+    }
+  }
+}
+
+function generateLogbackConfigFile(
+  logFile: string,
+  logLevel: string = 'INFO'
+): string {
+  const logbackConfig = `<?xml version="1.0" encoding="UTF-8"?>\n
+<configuration>
+    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
+        <file>${logFile}</file>
+        <encoder>
+            <pattern>[%date{ISO8601}] [%level] [%logger] [%marker] [%thread] - %msg MDC: {%mdc}%n</pattern>
+        </encoder>
+    </appender>
+    <root level="${logLevel}">
+        <appender-ref ref="FILE" />
+    </root>
+</configuration>
+`
+  const logbackConfigFile = path.join(
+    appDataPath,
+    `serv-${omegaEditPort}.logconf.xml`
+  )
+  rotateLogFiles(logFile)
+  fs.writeFileSync(logbackConfigFile, logbackConfig)
+  return logbackConfigFile // Return the path to the logback config file
+}
+
+async function serverStart(serverPort: number) {
+  await serverStop(serverPort)
+  const serverStartingText = `Ωedit server starting on port ${omegaEditPort}`
+  const statusBarItem = vscode.window.createStatusBarItem(
+    vscode.StatusBarAlignment.Left
+  )
+  statusBarItem.text = serverStartingText
+  statusBarItem.show()
+
+  let animationFrame = 0
+  const animationInterval = 400 // ms per frame
+  const animationFrames = ['', '.', '..', '...']
+  const animationIntervalId = setInterval(() => {
+    const frame = animationFrames[animationFrame % animationFrames.length]
+    statusBarItem.text = `${serverStartingText} ${frame}`
+    ++animationFrame
+  }, animationInterval)
+
+  const logConfigFile = generateLogbackConfigFile(
+    path.join(appDataPath, `serv-${omegaEditPort}.log`),
+    'INFO' // TODO: Make this configurable
+  )
+  if (!fs.existsSync(logConfigFile)) {
+    throw new Error(`Log config file '${logConfigFile}' not found`)
+  }
+
+  // Start the server and wait up to 10 seconds for it to start
+  const start = performance.now()

Review Comment:
   @scholarsmate This and line 205 where you make `duration` are what causes the tests to break. `performance.now` must not be available during the CLI tests. When I removed them locally my tests ran fine. I think just removing the stuff related to that is fine as the user may not really need to know how fast it came up



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@daffodil.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org