Skip to content

Commit 846a07b

Browse files
authored
fix(logging): log sam cli location only if changed #3428
Problem: 1. sam cli location is not logged correctly (`SAM CLI location: [object Object]`) 2. sam cli location log message appears redundantly in the logs. Solution: 1. fix log call 2. only log the location if it changed. before: 2023-05-08 05:06:29 [INFO]: SAM CLI location: /opt/homebrew/bin/sam 2023-05-08 05:06:29 [INFO]: SAM CLI location: /opt/homebrew/bin/sam 2023-05-08 05:06:29 [INFO]: SAM CLI location: /opt/homebrew/bin/sam 2023-05-08 05:06:30 [INFO]: SAM CLI location: /opt/homebrew/bin/sam ... 2023-05-08 05:06:32 [INFO]: SAM CLI location: /opt/homebrew/bin/sam after 2023-05-08 05:04:46 [INFO]: SAM CLI location: /opt/homebrew/bin/sam
1 parent 69dd4f4 commit 846a07b

File tree

4 files changed

+60
-8
lines changed

4 files changed

+60
-8
lines changed

src/shared/sam/cli/samCliLocator.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ abstract class BaseSamCliLocator {
5151
location = await this.getSystemPathLocation()
5252
}
5353

54-
this.logger.info(`SAM CLI location: ${location}`)
55-
5654
return location
5755
}
5856

src/shared/sam/cli/samCliSettings.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { fromExtensionManifest, migrateSetting, Settings } from '../../settings'
88
import { stripUndefined, toRecord } from '../../utilities/collectionUtils'
99
import { ClassToInterfaceType, keys } from '../../utilities/tsUtils'
1010
import { DefaultSamCliLocationProvider, SamCliLocationProvider } from './samCliLocator'
11+
import { onceChanged } from '../../utilities/functionUtils'
1112

1213
// TODO(sijaden): remove after a few releases
1314
export async function migrateLegacySettings() {
@@ -83,6 +84,8 @@ const description = {
8384
}
8485

8586
export class SamCliSettings extends fromExtensionManifest('aws.samcli', description) {
87+
protected static readonly logIfChanged = onceChanged((s: string) => getLogger().info(s))
88+
8689
public constructor(
8790
private readonly locationProvider: SamCliLocationProvider = new DefaultSamCliLocationProvider(),
8891
settings: ClassToInterfaceType<Settings> = Settings.instance
@@ -100,10 +103,12 @@ export class SamCliSettings extends fromExtensionManifest('aws.samcli', descript
100103
const fromConfig = this.get('location', '')
101104

102105
if (fromConfig) {
106+
SamCliSettings.logIfChanged(`SAM CLI location: ${fromConfig}`)
103107
return { path: fromConfig, autoDetected: false }
104108
}
105109

106110
const fromSearch = await this.locationProvider.getLocation()
111+
SamCliSettings.logIfChanged(`SAM CLI location: ${fromSearch?.path}`)
107112
return { path: fromSearch?.path, autoDetected: true }
108113
}
109114

src/shared/utilities/functionUtils.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function shared<T, U extends any[]>(fn: (...args: U) => Promise<T>): (...
3434
}
3535

3636
/**
37-
* Special-case of `memoize`. Ensures a function is executed only once.
37+
* Special-case of `memoize`: creates a function that is executed only once.
3838
*/
3939
export function once<T>(fn: () => T): () => T {
4040
let val: T
@@ -43,13 +43,32 @@ export function once<T>(fn: () => T): () => T {
4343
return () => (ran ? val : ((val = fn()), (ran = true), val))
4444
}
4545

46+
/**
47+
* Special-case of `memoize`: creates a function that runs only if the args
48+
* changed versus the previous invocation.
49+
*
50+
* @note See note on {@link memoize}
51+
*
52+
* TODO: use lib?: https://github.com/anywhichway/nano-memoize
53+
*/
54+
export function onceChanged<T, U extends any[]>(fn: (...args: U) => T): (...args: U) => T {
55+
let val: T
56+
let ran = false
57+
let prevArgs = ''
58+
59+
return (...args) => ((ran && prevArgs === args.map(String).join(':'))
60+
? val
61+
: ((val = fn(...args)), (ran = true), (prevArgs = args.map(String).join(':')), val))
62+
}
63+
4664
/**
4765
* Creates a new function that stores the result of a call.
4866
*
49-
* ### Important
50-
* This currently uses an extremely simple mechanism for creating keys from parameters.
67+
* @note This uses an extremely simple mechanism for creating keys from parameters.
5168
* Objects are effectively treated as a single key, while primitive values will behave as
5269
* expected with a few very uncommon exceptions.
70+
*
71+
* TODO: use lib?: https://github.com/anywhichway/nano-memoize
5372
*/
5473
export function memoize<T, U extends any[]>(fn: (...args: U) => T): (...args: U) => T {
5574
const cache: { [key: string]: T | undefined } = {}

src/test/shared/utilities/functionUtils.test.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
*/
55

66
import * as assert from 'assert'
7-
import { once, throttle } from '../../../shared/utilities/functionUtils'
7+
import { once, onceChanged, throttle } from '../../../shared/utilities/functionUtils'
88
import { installFakeClock } from '../../testUtil'
99

10-
describe('once', function () {
11-
it('does not execute sync functions returning void more than once', function () {
10+
describe('functionUtils', function () {
11+
it('once()', function () {
1212
let counter = 0
1313
const fn = once(() => void counter++)
1414

@@ -18,6 +18,36 @@ describe('once', function () {
1818
fn()
1919
assert.strictEqual(counter, 1)
2020
})
21+
22+
it('onceChanged()', function () {
23+
let counter = 0
24+
const arg2 = {}
25+
const arg2_ = { a: 42 }
26+
const fn = onceChanged((s: string, o: object) => void counter++)
27+
28+
fn('arg1', arg2)
29+
assert.strictEqual(counter, 1)
30+
31+
fn('arg1', arg2)
32+
fn('arg1', arg2)
33+
assert.strictEqual(counter, 1)
34+
35+
fn('arg1_', arg2)
36+
assert.strictEqual(counter, 2)
37+
38+
fn('arg1_', arg2)
39+
fn('arg1_', arg2)
40+
fn('arg1_', arg2)
41+
assert.strictEqual(counter, 2)
42+
43+
fn('arg1', arg2_)
44+
assert.strictEqual(counter, 3)
45+
46+
// TODO: bug/limitation: Objects are not discriminated.
47+
// TODO: use lib?: https://github.com/anywhichway/nano-memoize
48+
fn('arg1', arg2_)
49+
assert.strictEqual(counter, 3)
50+
})
2151
})
2252

2353
describe('throttle', function () {

0 commit comments

Comments
 (0)