Skip to content

Fix languages #620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 80 additions & 35 deletions packages/logger/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,20 @@ export class Time {
) { }
}

// tslint:disable-next-line no-any
export type FieldArray = Array<Field<any>>;

// Same as FieldArray but supports taking an object or undefined.
// `undefined` is allowed to make it easy to conditionally display a field.
// For example: `error && field("error", error)`
// For example: `error && field("error", error)`.
// For objects, the logger will iterate over the keys and turn them into
// instances of Field.
// tslint:disable-next-line no-any
export type FieldArray = Array<Field<any> | undefined>;
export type LogArgument = Array<Field<any> | undefined | object>;

// Functions can be used to remove the need to perform operations when the
// logging level won't output the result anyway.
export type LogCallback = () => [string, ...FieldArray];
export type LogCallback = () => [string, ...LogArgument];

/**
* Creates a time field
Expand Down Expand Up @@ -82,13 +88,19 @@ export abstract class Formatter {
public abstract tag(name: string, color: string): void;

/**
* Add string or arbitrary variable.
* Add string variable.
*/
public abstract push(arg: string, color?: string, weight?: string): void;
/**
* Add arbitrary variable.
*/
public abstract push(arg: any): void; // tslint:disable-line no-any

/**
* Add fields.
*/
// tslint:disable-next-line no-any
public abstract fields(fields: Array<Field<any>>): void;
public abstract fields(fields: FieldArray): void;

/**
* Flush out the built arguments.
Expand Down Expand Up @@ -120,6 +132,9 @@ export abstract class Formatter {
* Browser formatter.
*/
export class BrowserFormatter extends Formatter {
/**
* Add a tag.
*/
public tag(name: string, color: string): void {
this.format += `%c ${name} `;
this.args.push(
Expand All @@ -131,6 +146,9 @@ export class BrowserFormatter extends Formatter {
this.push(" ");
}

/**
* Add an argument.
*/
public push(arg: any, color: string = "inherit", weight: string = "normal"): void { // tslint:disable-line no-any
if (color || weight) {
this.format += "%c";
Expand All @@ -143,8 +161,11 @@ export class BrowserFormatter extends Formatter {
this.args.push(arg);
}

/**
* Add fields.
*/
// tslint:disable-next-line no-any
public fields(fields: Array<Field<any>>): void {
public fields(fields: FieldArray): void {
// tslint:disable-next-line no-console
console.groupCollapsed(...this.flush());
fields.forEach((field) => {
Expand All @@ -166,6 +187,9 @@ export class BrowserFormatter extends Formatter {
* Server (Node) formatter.
*/
export class ServerFormatter extends Formatter {
/**
* Add a tag.
*/
public tag(name: string, color: string): void {
const [r, g, b] = this.hexToRgb(color);
while (name.length < 5) {
Expand All @@ -175,6 +199,9 @@ export class ServerFormatter extends Formatter {
this.format += `\u001B[38;2;${r};${g};${b}m${name} \u001B[0m`;
}

/**
* Add an argument.
*/
public push(arg: any, color?: string, weight?: string): void { // tslint:disable-line no-any
if (weight === "bold") {
this.format += "\u001B[1m";
Expand All @@ -190,8 +217,11 @@ export class ServerFormatter extends Formatter {
this.args.push(arg);
}

/**
* Add fields.
*/
// tslint:disable-next-line no-any
public fields(fields: Array<Field<any>>): void {
public fields(fields: FieldArray): void {
// tslint:disable-next-line no-any
const obj: { [key: string]: any} = {};
this.format += "\u001B[38;2;140;140;140m";
Expand Down Expand Up @@ -257,80 +287,83 @@ export class Logger {
this.muted = true;
}

/**
* Extend the logger (for example to send the logs elsewhere).
*/
public extend(extender: Extender): void {
this.extenders.push(extender);
}

/**
* Outputs information.
* Log information.
*/
public info(fn: LogCallback): void;
public info(message: string, ...fields: FieldArray): void;
public info(message: LogCallback | string, ...fields: FieldArray): void {
public info(message: string, ...args: LogArgument): void;
public info(message: LogCallback | string, ...args: LogArgument): void {
this.handle({
type: "info",
message,
fields,
args,
tagColor: "#008FBF",
level: Level.Info,
});
}

/**
* Outputs a warning.
* Log a warning.
*/
public warn(fn: LogCallback): void;
public warn(message: string, ...fields: FieldArray): void;
public warn(message: LogCallback | string, ...fields: FieldArray): void {
public warn(message: string, ...args: LogArgument): void;
public warn(message: LogCallback | string, ...args: LogArgument): void {
this.handle({
type: "warn",
message,
fields,
args,
tagColor: "#FF9D00",
level: Level.Warning,
});
}

/**
* Outputs a trace message.
* Log a trace message.
*/
public trace(fn: LogCallback): void;
public trace(message: string, ...fields: FieldArray): void;
public trace(message: LogCallback | string, ...fields: FieldArray): void {
public trace(message: string, ...args: LogArgument): void;
public trace(message: LogCallback | string, ...args: LogArgument): void {
this.handle({
type: "trace",
message,
fields,
args,
tagColor: "#888888",
level: Level.Trace,
});
}

/**
* Outputs a debug message.
* Log a debug message.
*/
public debug(fn: LogCallback): void;
public debug(message: string, ...fields: FieldArray): void;
public debug(message: LogCallback | string, ...fields: FieldArray): void {
public debug(message: string, ...args: LogArgument): void;
public debug(message: LogCallback | string, ...args: LogArgument): void {
this.handle({
type: "debug",
message,
fields,
args,
tagColor: "#84009E",
level: Level.Debug,
});
}

/**
* Outputs an error.
* Log an error.
*/
public error(fn: LogCallback): void;
public error(message: string, ...fields: FieldArray): void;
public error(message: LogCallback | string, ...fields: FieldArray): void {
public error(message: string, ...args: LogArgument): void;
public error(message: LogCallback | string, ...args: LogArgument): void {
this.handle({
type: "error",
message,
fields,
args,
tagColor: "#B00000",
level: Level.Error,
});
Expand All @@ -350,29 +383,41 @@ export class Logger {
}

/**
* Outputs a message.
* Log a message.
*/
private handle(options: {
type: "trace" | "info" | "warn" | "debug" | "error";
message: string | LogCallback;
fields?: FieldArray;
args?: LogArgument;
level: Level;
tagColor: string;
}): void {
if (this.level > options.level || this.muted) {
return;
}

let passedFields = options.fields || [];
if (typeof options.message === "function") {
const values = options.message();
options.message = values.shift() as string;
passedFields = values as FieldArray;
options.args = values as FieldArray;
}

const fields = (this.defaultFields
? passedFields.filter((f) => !!f).concat(this.defaultFields)
: passedFields.filter((f) => !!f)) as Array<Field<any>>; // tslint:disable-line no-any
const fields: FieldArray = [];
if (options.args) {
options.args.forEach((arg) => {
if (arg instanceof Field) {
fields.push(arg);
} else if (arg) {
Object.keys(arg).forEach((k) => {
// tslint:disable-next-line no-any
fields.push(field(k, (arg as any)[k]));
});
}
});
}
if (this.defaultFields) {
fields.push(...this.defaultFields);
}

const now = Date.now();
let times: Array<Field<Time>> = [];
Expand Down Expand Up @@ -410,7 +455,7 @@ export class Logger {
this.extenders.forEach((extender) => {
extender({
section: this.name,
fields: options.fields,
fields,
level: options.level,
message: options.message as string,
type: options.type,
Expand Down
1 change: 1 addition & 0 deletions packages/protocol/src/browser/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ export class Client {
shell: init.getShell(),
extensionsDirectory: init.getExtensionsDirectory(),
builtInExtensionsDirectory: init.getBuiltinExtensionsDir(),
languageData: init.getLanguageData(),
};
this.initDataEmitter.emit(this._initData);
break;
Expand Down
1 change: 1 addition & 0 deletions packages/protocol/src/common/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface InitData {
readonly shell: string;
readonly extensionsDirectory: string;
readonly builtInExtensionsDirectory: string;
readonly languageData: string;
}

export interface SharedProcessData {
Expand Down
27 changes: 24 additions & 3 deletions packages/protocol/src/node/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ import { ChildProcessModuleProxy, ForkProvider, FsModuleProxy, NetModuleProxy, N

// tslint:disable no-any

export interface LanguageConfiguration {
locale: string;
}

export interface ServerOptions {
readonly workingDirectory: string;
readonly dataDirectory: string;
readonly cacheDirectory: string;
readonly builtInExtensionsDirectory: string;
readonly extensionsDirectory: string;
readonly fork?: ForkProvider;
readonly getLanguageData?: () => Promise<LanguageConfiguration>;
}

interface ProxyData {
Expand Down Expand Up @@ -99,9 +104,25 @@ export class Server {
initMsg.setTmpDirectory(os.tmpdir());
initMsg.setOperatingSystem(platformToProto(os.platform()));
initMsg.setShell(os.userInfo().shell || global.process.env.SHELL || "");
const srvMsg = new ServerMessage();
srvMsg.setInit(initMsg);
connection.send(srvMsg.serializeBinary());

const getLanguageData = this.options.getLanguageData
|| ((): Promise<LanguageConfiguration> => Promise.resolve({
locale: "en",
}));

getLanguageData().then((languageData) => {
try {
initMsg.setLanguageData(JSON.stringify(languageData));
} catch (error) {
logger.error("Unable to send language config", field("error", error));
}

const srvMsg = new ServerMessage();
srvMsg.setInit(initMsg);
connection.send(srvMsg.serializeBinary());
}).catch((error) => {
logger.error(error.message, field("error", error));
});
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/protocol/src/proto/client.proto
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ message WorkingInit {
string shell = 6;
string builtin_extensions_dir = 7;
string extensions_directory = 8;
string language_data = 9;
}
4 changes: 4 additions & 0 deletions packages/protocol/src/proto/client_pb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ export class WorkingInit extends jspb.Message {
getExtensionsDirectory(): string;
setExtensionsDirectory(value: string): void;

getLanguageData(): string;
setLanguageData(value: string): void;

serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): WorkingInit.AsObject;
static toObject(includeInstance: boolean, msg: WorkingInit): WorkingInit.AsObject;
Expand All @@ -155,6 +158,7 @@ export namespace WorkingInit {
shell: string,
builtinExtensionsDir: string,
extensionsDirectory: string,
languageData: string,
}

export enum OperatingSystem {
Expand Down
Loading