Skip to content

Commit

Permalink
Rebased
Browse files Browse the repository at this point in the history
  • Loading branch information
rsercano committed Oct 23, 2020
2 parents c42d58c + 8d10df0 commit a3910eb
Show file tree
Hide file tree
Showing 18 changed files with 1,019 additions and 272 deletions.
46 changes: 46 additions & 0 deletions docs/api.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 17 additions & 1 deletion lib/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ const LevelPriorities = {
trace: 6,
};

enum LevelPriority {
alert,
error,
warn,
info,
verbose,
debug,
trace,
}

export enum Context {
Global = 'GLOBAL',
DB = 'DB',
Expand Down Expand Up @@ -138,6 +148,12 @@ class Logger {
});
}

public setLogLevel = (level: Level) => {
this.logger?.transports.forEach((transport) => {
transport.level = level;
});
}

private getLogFormat = (colorize: boolean, dateFormat?: string) => {
const { format } = winston;

Expand Down Expand Up @@ -219,4 +235,4 @@ class Logger {
}

export default Logger;
export { Level, Loggers };
export { Level, Loggers, LevelPriority };
7 changes: 7 additions & 0 deletions lib/Xud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ class Xud extends EventEmitter {
shutdown: this.beginShutdown,
});

this.service.on('logLevel', (level) => {
this.swapClientManager?.setLogLevel(level);
Object.values(loggers).forEach((logger) => {
logger.setLogLevel(level);
});
});

if (this.swapClientManager.connextClient?.isOperational()) {
this.httpServer = new HttpServer(loggers.http, this.service);
await this.httpServer.listen(
Expand Down
29 changes: 29 additions & 0 deletions lib/cli/commands/loglevel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Arguments, Argv } from 'yargs';
import { LogLevel, SetLogLevelRequest } from '../../proto/xudrpc_pb';
import { callback, loadXudClient } from '../command';
import { LevelPriority } from '../../Logger';

export const command = 'loglevel <level>';

export const describe = 'set the logging level for xud';

export const builder = (argv: Argv) => argv
.positional('level', {
description: 'the logging level',
type: 'string',
choices: ['alert', 'error', 'warn', 'verbose', 'info', 'debug', 'trace'],
coerce: (logLevelStr: string) => {
const logLevelLower = logLevelStr.toLowerCase();
return logLevelLower;
},
})
.example('$0 loglevel trace', 'set log level to trace')
.example('$0 loglevel info', 'set log level to info');

export const handler = async (argv: Arguments<any>) => {
const request = new SetLogLevelRequest();
const levelPriority = LevelPriority[argv.level] as unknown as number;
const logLevel: LogLevel = levelPriority as LogLevel;
request.setLogLevel(logLevel);
(await loadXudClient(argv)).setLogLevel(request, callback(argv));
};
2 changes: 1 addition & 1 deletion lib/cli/commands/unlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const describe = 'unlock local xud node';
export const builder = {};

const formatOutput = (response: UnlockNodeResponse.AsObject) => {
console.log('xud was unlocked succesfully');
console.log('xud was unlocked successfully');
if (response.unlockedLndsList.length) {
console.log(`The following wallets were unlocked: ${response.unlockedLndsList.join(', ')}`);
}
Expand Down
14 changes: 14 additions & 0 deletions lib/grpc/GrpcService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,20 @@ class GrpcService {
}
}

public setLogLevel: grpc.handleUnaryCall<xudrpc.SetLogLevelRequest, xudrpc.SetLogLevelResponse> = async (call, callback) => {
if (!this.isReady(this.service, callback)) {
return;
}
try {
await this.service.setLogLevel(call.request.toObject());

const response = new xudrpc.SetLogLevelResponse();
callback(null, response);
} catch (err) {
callback(getGrpcError(err), null);
}
}

public shutdown: grpc.handleUnaryCall<xudrpc.ShutdownRequest, xudrpc.ShutdownResponse> = (_, callback) => {
if (!this.isReady(this.service, callback)) {
return;
Expand Down
1 change: 1 addition & 0 deletions lib/grpc/getGrpcError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const getGrpcError = (err: any) => {
case orderErrorCodes.MIN_QUANTITY_VIOLATED:
case orderErrorCodes.QUANTITY_DOES_NOT_MATCH:
case swapErrorCodes.REMOTE_IDENTIFIER_MISSING:
case orderErrorCodes.DUPLICATE_PAIR_CURRENCIES:
code = status.INVALID_ARGUMENT;
break;
case orderErrorCodes.PAIR_DOES_NOT_EXIST:
Expand Down
3 changes: 3 additions & 0 deletions lib/orderbook/OrderBook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ class OrderBook extends EventEmitter {

public addPair = async (pair: Pair) => {
const pairId = derivePairId(pair);
if (pair.baseCurrency.toLowerCase() === pair.quoteCurrency.toLowerCase()) {
throw errors.DUPLICATE_PAIR_CURRENCIES(pair.baseCurrency, pair.quoteCurrency);
}
if (this.pairInstances.has(pairId)) {
throw errors.PAIR_ALREADY_EXISTS(pairId);
}
Expand Down
5 changes: 5 additions & 0 deletions lib/orderbook/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const errorCodes = {
INSUFFICIENT_OUTBOUND_BALANCE: codesPrefix.concat('.12'),
MIN_QUANTITY_VIOLATED: codesPrefix.concat('.13'),
QUANTITY_ON_HOLD: codesPrefix.concat('.15'),
DUPLICATE_PAIR_CURRENCIES: codesPrefix.concat('.16'),
};

const errors = {
Expand Down Expand Up @@ -75,6 +76,10 @@ const errors = {
message: `order with local id ${localId} has a quantity of ${holdQuantity} satoshis on hold, try again later`,
code: errorCodes.QUANTITY_DOES_NOT_MATCH,
}),
DUPLICATE_PAIR_CURRENCIES: (baseCurrency: string, quoteCurrency: string) => ({
message: `base asset (${baseCurrency}) and quote asset (${quoteCurrency}) have to be different`,
code: errorCodes.DUPLICATE_PAIR_CURRENCIES,
}),
};

export { errorCodes };
Expand Down
51 changes: 51 additions & 0 deletions lib/proto/xudrpc.swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions lib/proto/xudrpc_grpc_pb.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a3910eb

Please sign in to comment.